Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum depth of an Array in JavaScript

Tags:

So, I was experimented one day when I came across this question in Stack Overflow and got curious: Maximum size of an Array in Javascript (Maximum size of an Array in JavaScript).

Question is, what is the Maximum depth of an Array in JavaScript?

By depth I mean how much can you nest an array within an array until JavaScript gives up?

[1]           // Depth Level: 1
[1, [2]]      // Depth Level: 2
[1, [2, [3]]] // Depth Level: 3
[1, [...]]    // Depth Level: x?

Is it machine-dependent, is it based on the compiler?

No, I'm not sure if there's any practical use for this but it's still something I'm curious about nonetheless.

like image 414
Lapys Avatar asked Jun 27 '18 21:06

Lapys


People also ask

How do you find the depth of an array?

If current_max is greater than max then update max value to current_max at that instance. after traverse is completed the max is is the depth of the array.

How do you find the maximum length of an array?

Max SizeIt generally depends on the JVM that we're using and the platform. Since the index of the array is int, the approximate index value can be 2^31 – 1. Based on this approximation, we can say that the array can theoretically hold 2,147,483,647 elements.

How long can an array in JavaScript?

The exact maximum limit of an array is 2^32 - 1 or 4294967295, due to restrictions in Javascript's memory. The number of items, also known as the length property, cannot be greater than that.

Can you change the size of an array in JavaScript?

For traditional “dense” arrays that have contiguous elements and begin with element 0, the length property specifies the number of elements in the array. You can set the value of the length property to change the size of an array.


2 Answers

Generally you can nest different arrays until you run out of memory, but you can nest the same array and get an effectively infinite depth array.

var x = [ ];
x.push(x);

x[0][0][0](...)[0]; // Now valid

This shows up as [ [Circular] ] in most debuggers because it's what's called a "circular reference", as in the array contains itself.

This is similar to how you can have self-referential objects:

var x = { };
x.x = x;

// { x: [Circular] }

x.x.x.x.x.x.x.x.x(...).x; // Now valid

JavaScript itself doesn't really care what depth something is, the language has no intrinsic limit. Sometimes circular references are a feature, like x.y refers to something that refers back to x for convenience, a way of showing mutual association. That's technically infinite depth, but it's unlikely you'd use it that way.

Here's a simple example of that phenomenon:

var x = { };
var y = { };

x.y = y;
y.x = x;

x.y.x.y.x.y; // Pointless, but valid.
like image 85
tadman Avatar answered Sep 28 '22 19:09

tadman


tadman gave an answer from the runtime perspective (infinite depth), but from a compile-time perspective, there is a limit. It's not one that's built into JavaScript (as far as I know), but consider what the compiler has to do to compile your code.

[1, [2, [3]]]

The compiler needs to turn the above expression into an abstract syntax tree. Roughly:

[ , ]
 /\
1 [ , ]
   /\
  2 [3]

If the compiler runs out of memory when creating those nodes, because there are too many nodes/expressions, then it will crash and your program won't compile. In the case of JavaScript, since it uses a JIT, I guess it's a compilation error that happens at runtime.

like image 38
pushkin Avatar answered Sep 28 '22 19:09

pushkin