简体   繁体   English

Javascript 中可能有多少个嵌套对象或 Arrays?

[英]How many nested Objects or Arrays are possible in Javascript?

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?这个问题是出于好奇,但是给定的 Object 可以有多少嵌套对象(或者类似地,因为typeof [] === "object" ,数组中可以嵌套多少个 Arrays)在 JS 中有限制吗?

I thought about creating an array that could have thousands of nested arrays, with thousands of numbers inside them as well.我想过创建一个数组,它可以有数千个嵌套的 arrays,其中也有数千个数字。 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, eg, too much recursion or nested for-loops, or hardware limitations).撇开可能的最佳实践问题不谈,我想知道我是否会在某个时候遇到上限(也就是说,假设这不仅仅是一个问题,例如太多的递归或嵌套 for 循环,或硬件限制)。

Related: how many nested object should i define in javascript?相关: 我应该在 javascript 中定义多少个嵌套的 object?

When you create an object or array in Javascript, it allocates a small block of memory to store the value.当你在 Javascript 中创建一个 object 或数组时,它会分配一个 memory 的小块来存储值。 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. object 或数组中的所有值,包括其他对象或 arrays,都分配了它们自己的单独块 memory,并引用父 object 或数组中的地址。

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.因此,您一次可以在脚本中初始化多少个嵌套对象或 arrays(或多少个对象或 arrays,句号),完全取决于您的计算机有多少 RAM。 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.一旦您开始达到 RAM 容量的极限,处理器速度就会发挥作用,因为您的计算机定位、创建和重新分配地址空间的速度越快,系统在崩溃之前可以运行的时间就越长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM