diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp
index 5a66a860..eecff7fc 100644
--- a/include/nlohmann/json.hpp
+++ b/include/nlohmann/json.hpp
@@ -1004,10 +1004,12 @@ class basic_json
             // move the top-level items to stack
             if (t == value_t::array)
             {
+                stack.reserve(array->size());
                 std::move(array->begin(), array->end(), std::back_inserter(stack));
             }
             else if (t == value_t::object)
             {
+                stack.reserve(object->size());
                 for (auto&& it : *object)
                 {
                     stack.push_back(std::move(it.second));
diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp
index 8acb1cc0..f9be0b4e 100644
--- a/single_include/nlohmann/json.hpp
+++ b/single_include/nlohmann/json.hpp
@@ -15547,10 +15547,12 @@ class basic_json
             // move the top-level items to stack
             if (t == value_t::array)
             {
+                stack.reserve(array->size());
                 std::move(array->begin(), array->end(), std::back_inserter(stack));
             }
             else if (t == value_t::object)
             {
+                stack.reserve(object->size());
                 for (auto&& it : *object)
                 {
                     stack.push_back(std::move(it.second));