diff --git a/include/nlohmann/detail/iterators/iteration_proxy.hpp b/include/nlohmann/detail/iterators/iteration_proxy.hpp
index 39c35a2e..047e1855 100644
--- a/include/nlohmann/detail/iterators/iteration_proxy.hpp
+++ b/include/nlohmann/detail/iterators/iteration_proxy.hpp
@@ -21,8 +21,10 @@ template<typename IteratorType> class iteration_proxy
         IteratorType anchor;
         /// an index for arrays (used to create key names)
         std::size_t array_index = 0;
+        /// last stringified array index
+        mutable std::size_t array_index_last = 0;
         /// a string representation of the array index
-        std::string array_index_str = "0";
+        mutable std::string array_index_str = "0";
         /// an empty string (to return a reference for primitive values)
         const std::string empty_str = "";
 
@@ -39,13 +41,7 @@ template<typename IteratorType> class iteration_proxy
         iteration_proxy_internal& operator++()
         {
             ++anchor;
-
-            assert(anchor.m_object != nullptr);
-            if (anchor.m_object->is_array())
-            {
-                // update array index and string representation
-                array_index_str = std::to_string(++array_index);
-            }
+            ++array_index;
 
             return *this;
         }
@@ -65,7 +61,14 @@ template<typename IteratorType> class iteration_proxy
             {
                 // use integer array index as key
                 case value_t::array:
+                {
+                    if (array_index != array_index_last)
+                    {
+                        array_index_str = std::to_string(array_index);
+                        array_index_last = array_index;
+                    }
                     return array_index_str;
+                }
 
                 // use key from the object
                 case value_t::object:
diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp
index 0b2e055c..24d2269a 100644
--- a/single_include/nlohmann/json.hpp
+++ b/single_include/nlohmann/json.hpp
@@ -4691,8 +4691,10 @@ template<typename IteratorType> class iteration_proxy
         IteratorType anchor;
         /// an index for arrays (used to create key names)
         std::size_t array_index = 0;
+        /// last stringified array index
+        mutable std::size_t array_index_last = 0;
         /// a string representation of the array index
-        std::string array_index_str = "0";
+        mutable std::string array_index_str = "0";
         /// an empty string (to return a reference for primitive values)
         const std::string empty_str = "";
 
@@ -4709,13 +4711,7 @@ template<typename IteratorType> class iteration_proxy
         iteration_proxy_internal& operator++()
         {
             ++anchor;
-
-            assert(anchor.m_object != nullptr);
-            if (anchor.m_object->is_array())
-            {
-                // update array index and string representation
-                array_index_str = std::to_string(++array_index);
-            }
+            ++array_index;
 
             return *this;
         }
@@ -4735,7 +4731,14 @@ template<typename IteratorType> class iteration_proxy
             {
                 // use integer array index as key
                 case value_t::array:
+                {
+                    if (array_index != array_index_last)
+                    {
+                        array_index_str = std::to_string(array_index);
+                        array_index_last = array_index;
+                    }
                     return array_index_str;
+                }
 
                 // use key from the object
                 case value_t::object: