24

We have an object with more than 75000 properties. The format of the object is as following:

// The key starts with 3 letters and then is followed by 8 numbers
var bigArray = {'AAA########':123456789,
                'AAA########':123456790,
                'AAA########':123456791
               }; 

Is there a known limit of the quantity of properties for JavaScript objects? From my tests the object still works at 65500 elements.

  • In Windows 7, IE9 the script crashes (error -2147024882).
  • Windows XP, IE8 works fine.
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
David Laberge
  • 15,435
  • 14
  • 53
  • 83

6 Answers6

19

In the current version of Chrome (Sept 2017), I'm limited to around 8.3 million keys. Try pasting this in your browser console:

let obj = {};
let keyCount = 0;
while(1) {
  obj[Math.random()] = Math.random();
  if(++keyCount % 10000 === 0) console.log(keyCount);
}

I get an identical limit in Node.js:

node --max-old-space-size=20000 -e "let obj = {}; let keyCount = 0; while(1) { obj[Math.random()] = Math.random(); if(++keyCount % 10000 === 0) console.log(keyCount); }"

Interestingly, if I use a Map, I can get about 16.8 million keys before it crashes (you can get past this limit with something like this).

  • 3
    It's 2020 now and I'm getting around 8.3 million as well. – The Qodesmith Mar 12 '20 at 01:15
  • For the Map, the 16.8 million limit may be basically this 2^24 limit https://bugs.chromium.org/p/v8/issues/detail?id=11852 and for the Object, the 8.3M may be 2^23 on the object (and in this case it seems to hang more than produce a specific error message) – Colin D Aug 15 '21 at 01:25
  • The limit for me with your browser console code was 16 million but it was 285.2 million when I used integer keys. 232 million when I used bigint keys which I was surprised made any difference since I thought keys were converted to strings before they are added as property names. Guess not. – PHP Guru Feb 19 '23 at 10:43
  • Does this mean each nested object can also have 16.8 million keys? – inux Jun 13 '23 at 08:00
9

I'm not sure what the actual value is, but I see the practical upper limit around 400,000 in node.js (on a Mac with 16 GB of RAM).

Here is a log of me adding rows from a database into an object:

[[21:32:34.325]] [LOG] 340001, pint of delight
[[21:32:35.574]] [LOG] 350001, pound shrimp
[[21:32:36.545]] [LOG] 360001, ravioli allaragosta
[[21:32:37.721]] [LOG] 370001, roasted ham and cheese
[[21:32:39.862]] [LOG] 380001, salmon kama
[[21:32:41.152]] [LOG] 390001, scallops and vegetables
[[21:32:42.150]] [LOG] 400001, show cabernet ca
[[21:32:44.412]] [LOG] 410001, sloppy nachos
[[21:33:25.425]] [LOG] 420001, spaghetti or ziti sauce
[[21:35:37.839]] [LOG] 430001, steak au poivre vert
[[21:37:37.202]] [LOG] 440001, sushi moriawase
[[21:39:45.365]] [LOG] 450001, tequila shooters
[[21:42:09.036]] [LOG] 460001, toro roll with scallion
[[21:44:32.796]] [LOG] 470001, two enchiladas taco rice and refried beans
[[21:47:02.584]] [LOG] 480001, veuve clicquot ponsardin rose reims nv
[[21:49:04.020]] [LOG] 490001, whole turkey gourmet sides
[[21:51:15.264]] [LOG] finished

Until around 400,000 it takes about 1 second to insert 10,000 new records. Past 410,000, the time increases almost exponentially.

I'm not sure how I'll solve this. Maybe make 2 objects and limit them to 400,000 keys each... a bit labor-intensive but better than rewriting a dictionary object :)

Update: It looks like it's actually the amount of memory used that is the issue and not so much the number of objects. My machine slows to a crawl at about 1.5 GB of RAM used. It might be linked to the memory allocated to the node.js process, which can be increased with this parameter: --max_old_space_size=4096 (number is in MB).

Nico
  • 4,248
  • 1
  • 20
  • 19
5

The exact maximum limit of an array is 2^32 - 1 or 4294967295, due to restrictions in Javascript's memory.

Link

talnicolas
  • 13,885
  • 7
  • 36
  • 56
5

From our test on this issue it seems that IE9, Windows 7, limits the number of line in a HTA script to 65535. I did not find any source on the issue, it is just the results of our tests.

David Laberge
  • 15,435
  • 14
  • 53
  • 83
4

It's going to be 2^32 - 1; however, specific browsers may limit it further.

crush
  • 16,713
  • 9
  • 59
  • 100
2

Tested with Deno version 1.6.2 on Windows 10 with 16gb ram.

Javascript objects maximum number of keys: 116.597.277

deno eval "let obj = {}; let keyCount = 0; while(1) { ++keyCount; obj[keyCount] = keyCount; if(keyCount % 10000 === 0 || keyCount > 116590000) console.log(keyCount); }"

Map maximum number of keys: 116.597.277

deno eval "let obj = new Map(); let keyCount = 0; while(1) { ++keyCount; obj[keyCount] = keyCount; if(keyCount % 10000 === 0 || keyCount > 116590000) console.log(keyCount); }"

I have obtained the same numbers testing Javascript objects and Map with Edge version 87.0.664.66 and Chrome version 87.0.4280.88.

Stefano Spinucci
  • 554
  • 6
  • 13
  • 1
    I think what you're seeing here is a behind-the-scenes optimisation given that you're using sequential integers as keys. If you use random keys you'll still get themuch lower 8.3 million limit, as of March 2021 (just tested in Deno, Node, and Chromium). – joe Mar 25 '21 at 08:02
  • 2
    @joe you're right, simply adding a string to the key (changing `obj[keyCount] = keyCount;` to `obj['a'+keyCount] = keyCount;`) gives me the same result of 8,3 million (the code simply stops working, doesn't exit with an exception with the integer keys) – Stefano Spinucci Mar 26 '21 at 17:05
  • see also here for chrome/nodejs/v8 https://bugs.chromium.org/p/v8/issues/detail?id=11852 – Colin D Aug 15 '21 at 01:24