1

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.

Community
  • 1
  • 1
Bernhard
  • 4,855
  • 5
  • 39
  • 70
  • In Chrome maximum array size is something between 1e9 and 1e10. – Michał Perłakowski May 24 '16 at 19:04
  • And crashing is not related to the size of array, but to the number of iteration. It would crash with a normal loop too. – Michał Perłakowski May 24 '16 at 19:05
  • Why are you iterating through 100 million items in JS? Are you really using the best approach? – Marcos Lima May 24 '16 at 19:05
  • Regardless of the maximum length an array can have I am sure there are limits to how much RAM an array takes. So you could have an array with `length = 1` but if that 1 item takes up a lot of RAM then it might not work... – IMTheNachoMan May 24 '16 at 19:06
  • If you explain what you're trying to do (and/or why) then we might be able to provide a better answer than a 100+ million iteration. – IMTheNachoMan May 24 '16 at 19:07
  • @Gothodo When I'm removing any operation in the for loop the done is reached. So the loop is running – Bernhard May 24 '16 at 19:08
  • @MarcosLima I'm working with the javascript filereader api but reduced the error to this loop (the amount of iterations is related to the size of the file being processed) – Bernhard May 24 '16 at 19:10
  • As initially written the array is smaller as the maximum possible size best voted in the linked question – Bernhard May 24 '16 at 19:11
  • Interesting... 11184810 decimal is #AAAAAA hex = 101010101010101010101010 bin, which suggests it could be some special value – amaksr May 24 '16 at 19:25
  • @amaksr yes that's interesting but when i set the values directly without the iteration it's working. – Bernhard May 24 '16 at 19:30
  • 1
    I have chrome crashing or just not loading my script with for loop through array with over million elements. Seems 999,999 works but 1,000,000 crash? – marshal craft Aug 22 '18 at 10:31

1 Answers1

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

Community
  • 1
  • 1
gizmo
  • 82
  • 1
  • 5