javascript中的大型数组是否有项目限制 [英] Is there an item limit for large arrays in javascript

查看:49
本文介绍了javascript中的大型数组是否有项目限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经阅读了此问题,但并未涉及可以理解的答案.

Already read this Question but didn't come to an understandable answer.

我有一个预定义大小的数组 new Array(105119296).然后,在定义数组之后,我开始了一个循环,以向每个索引填充一个值.此过程通常在Webworker中运行,但会在那里崩溃以及直接在浏览器中崩溃.

I have an array new Array(105119296) with a predefined size. Then after the array was defined I started a loop to fill each index with a value. This process normally runs in a webworker but crashes there as well as in the browser directly.

Chrome Mac 50.0.2661.102(64位)中进行11184811迭代之后,执行崩溃.

After 11184811 iterations in Chrome Mac 50.0.2661.102 (64-bit) the execution crashes.

下面的脚本再现了这种情况.

The script below reproduces the situation.

 var len = 105119296;
 var arr = new Array(len);

 for(var i=0;i<len;i++){

   var data = 0;// Math.round(Math.random()*10);

   if(i>11184810){
     console.log(i + '->' + data);
     // At 11184811 Chrome dev tool crashes
   }

  arr[i] = data;

}

console.log('done');

我的一般问题是:

在javascript中数组可以容纳的大小是否有限制?而且,如果不是这样的话,为什么这样的事情在Webworker中无法正常运行,据我了解,这些繁琐的任务会阻塞浏览器的视图.

Is there a limit to the size an array can hold in javascript? And if not why is something like this not running properly in a webworker which is for my understanding for heavy tasks which would block the browsers view.

推荐答案

我在类似的问题中找到了这个答案: JavaScript中数组的最大大小

I found this answer in a similar question: Maximum size of an Array in Javascript

直到变慢"的最大长度.完全取决于目标计算机和实际代码,因此您需要在该平台上进行测试,以了解可接受的条件.

The maximum length until "it gets sluggish" is totally dependent on your target machine and your actual code, so you'll need to test on that (those) platform(s) to see what is acceptable.

但是,由于ToUint32抽象操作,根据ECMA-262第5版规范的数组的最大长度由无符号的32位整数限制,因此最长的数组可能具有2 ^ 32-1 = 4,294,967,295= 42.9亿个元素.

However, the maximum length of an array according to the ECMA-262 5th Edition specification is bound by an unsigned 32-bit integer due to the ToUint32 abstract operation, so the longest possible array could have 2^32-1 = 4,294,967,295 = 4.29 billion elements.

这篇关于javascript中的大型数组是否有项目限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆