Maximum size of an Array in Javascript

前端 未结 7 1761
我在风中等你
我在风中等你 2020-11-22 10:46

Context: I\'m building a little site that reads an rss feed, and updates/checks the feed in the background. I have one array to store data to display, and another which stor

相关标签:
7条回答
  • 2020-11-22 11:02

    I have built a performance framework that manipulates and graphs millions of datasets, and even then, the javascript calculation latency was on order of tens of milliseconds. Unless you're worried about going over the array size limit, I don't think you have much to worry about.

    0 讨论(0)
  • 2020-11-22 11:02

    I have shamelessly pulled some pretty big datasets in memory, and altough it did get sluggish it took maybe 15 Mo of data upwards with pretty intense calculations on the dataset. I doubt you will run into problems with memory unless you have intense calculations on the data and many many rows. Profiling and benchmarking with different mock resultsets will be your best bet to evaluate performance.

    0 讨论(0)
  • 2020-11-22 11:04

    Like @maerics said, your target machine and browser will determine performance.

    But for some real world numbers, on my 2017 enterprise Chromebook, running the operation:

    console.time();
    Array(x).fill(0).filter(x => x < 6).length
    console.timeEnd();
    
    • x=5e4 takes 16ms, good enough for 60fps
    • x=4e6 takes 250ms, which is noticeable but not a big deal
    • x=3e7 takes 1300ms, which is pretty bad
    • x=4e7 takes 11000ms and allocates an extra 2.5GB of memory

    So around 30 million elements is a hard upper limit, because the javascript VM falls off a cliff at 40 million elements and will probably crash the process.

    0 讨论(0)
  • 2020-11-22 11:14

    You could try something like this to test and trim the length:

    http://jsfiddle.net/orolo/wJDXL/

    var longArray = [1, 2, 3, 4, 5, 6, 7, 8];
    
    if (longArray.length >= 6) {
      longArray.length = 3;
    }
    
    alert(longArray); //1, 2, 3

    0 讨论(0)
  • 2020-11-22 11:17

    No need to trim the array, simply address it as a circular buffer (index % maxlen). This will ensure it never goes over the limit (implementing a circular buffer means that once you get to the end you wrap around to the beginning again - not possible to overrun the end of the array).

    For example:

    var container = new Array ();
    var maxlen = 100;
    var index = 0;
    
    // 'store' 1538 items (only the last 'maxlen' items are kept)
    for (var i=0; i<1538; i++) {
       container [index++ % maxlen] = "storing" + i;
    }
    
    // get element at index 11 (you want the 11th item in the array)
    eleventh = container [(index + 11) % maxlen];
    
    // get element at index 11 (you want the 11th item in the array)
    thirtyfifth = container [(index + 35) % maxlen];
    
    // print out all 100 elements that we have left in the array, note
    // that it doesn't matter if we address past 100 - circular buffer
    // so we'll simply get back to the beginning if we do that.
    for (i=0; i<200; i++) {
       document.write (container[(index + i) % maxlen] + "<br>\n");
    }
    
    0 讨论(0)
  • 2020-11-22 11:22

    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 232-1 = 4,294,967,295 = 4.29 billion elements.

    0 讨论(0)
提交回复
热议问题