0

This question comes out of curiosity, but is there a limit to how many nested Objects a given Object can have (or similarly, since typeof [] === "object", how many Arrays can be nested in an Array) in JS?

I thought about creating an array that could have thousands of nested arrays, with thousands of numbers inside them as well. Setting aside possible best practice issues, I was wondering whether I could run into a cap at some point (that is, supposing it's not just a problem with, e.g., too much recursion or nested for-loops, or hardware limitations).

Related: how many nested object should i define in javascript?

flen
  • 1,905
  • 1
  • 21
  • 44

1 Answers1

2

When you create an object or array in Javascript, it allocates a small block of memory to store the value. All values in the object or array, including other objects or arrays, are allocated their own separate block of memory, with a reference to the address in the parent object or array.

Therefore, how many nested objects or arrays (or how many objects or arrays, period) that you can have initialized in a script at one time, is entirely dependent on how much RAM your computer has. Once you start to reach the limits of your RAM capacity, processor speed comes into play, as the faster your computer can locate, create, and reallocate address space, the longer the system can run before it crashes.

symlink
  • 11,984
  • 7
  • 29
  • 50
  • 2
    The contents of the array are also pointers under the hood, unless it is an integer array (for some JS implementations). So each array element points to another memory block. – Chill Apr 24 '20 at 23:45
  • @Chill Yes good point, I've updated my answer to include that detail. – symlink Apr 24 '20 at 23:57