JavaScript中数组的最大深度

时间:2022-07-05 21:21:25

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).

所以,有一天,当我在Stack Overflow中遇到这个问题时,我进行了实验,并对此感到好奇:Javascript中数组的最大大小(JavaScript中数组的最大大小)。

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

问题是,JavaScript中数组的最大深度是多少?

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

深度我的意思是,在JavaScript放弃之前,您可以将数组嵌套在数组中多少?

[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.

不,我不确定这是否有任何实际用途,但它仍然是我很好奇的东西。

2 个解决方案

#1


3  

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.

这在大多数调试器中显示为[[Circular]],因为它就是所谓的“循环引用”,就像在数组中包含它一样。

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.

JavaScript本身并不关心什么是深度,语言没有内在限制。有时循环引用是一个特征,就像x.y指的是为了方便而引用x的东西,一种显示相互关联的方式。这在技术上是无限的深度,但你不太可能以这种方式使用它。

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.

#2


1  

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.

tadman从运行时角度(无限深度)给出了答案,但从编译时的角度来看,存在一个限制。它不是内置于JavaScript中的(据我所知),但请考虑编译器编译代码时必须做的事情。

[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.

如果编译器在创建这些节点时耗尽内存,因为节点/表达式太多,那么它将崩溃并且您的程序将无法编译。在JavaScript的情况下,因为它使用JIT,我想这是在运行时发生的编译错误。

#1


3  

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.

这在大多数调试器中显示为[[Circular]],因为它就是所谓的“循环引用”,就像在数组中包含它一样。

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.

JavaScript本身并不关心什么是深度,语言没有内在限制。有时循环引用是一个特征,就像x.y指的是为了方便而引用x的东西,一种显示相互关联的方式。这在技术上是无限的深度,但你不太可能以这种方式使用它。

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.

#2


1  

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.

tadman从运行时角度(无限深度)给出了答案,但从编译时的角度来看,存在一个限制。它不是内置于JavaScript中的(据我所知),但请考虑编译器编译代码时必须做的事情。

[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.

如果编译器在创建这些节点时耗尽内存,因为节点/表达式太多,那么它将崩溃并且您的程序将无法编译。在JavaScript的情况下,因为它使用JIT,我想这是在运行时发生的编译错误。