Is there an item limit for large arrays in javascript

陌路散爱 提交于 2021-02-11 07:42:58

问题


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

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.

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');

My question in general is:

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.


回答1:


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.

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.



来源:https://stackoverflow.com/questions/37421749/is-there-an-item-limit-for-large-arrays-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!