Today I Learned - Rocky Kev

TIL that the max size of a Map/Set is 26843544

POSTED ON:

TAGS:

In JavaScript, there's the Map and the Set object.

Set: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
Map: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

via https://bugs.chromium.org/p/v8/issues/detail?id=11852

According to this bug report on the Chromium's V8 board, which is Google's open-source Javascript engine, there's a limit to how large Set objects can be.

Is there a specific reason why a Set has a limit of 2^24 elements?

(2^24 equals 16777216)

Map too!

const superSet = new Map();
for (let i = 0; i <= 16777216; i++) superSet.set(i, i);

The answer, via syg@chromium.org:

JS Maps and Sets are implemented by OrderedHashTable, and the limit you're running into is in [1]. OrderedHashTables double in size when they need to grow, and their max size is 26843544. After 16777216 elements, doubling that exceeds the max, so the allocation fails.

Currently these data structures are not implemented to be usable for large in-memory datasets. Also note that the JS specification itself doesn't require that Maps and Sets must scale with available memory.

Well dang, they're right. https://tc39.es/ecma262/#sec-set-objects


Related TILs

Tagged:

TIL what is npm Script

Despite their high usage they are not particularly well optimized and add about 400ms of overhead. In this article we were able to bring that down to ~22ms.

TIL fancy methods to transform Javascript Objects

You can use Object.entries(), Object.keys(), Object.fromEntries()...

TIL how to hide your JS code

ONE THING TO NOTE: Encrypting a script is stronger than obfuscation, both methods are still not adequate to protect secret content. Honestly, I don't think it's worth it on a production site, and instead just go with pure server-side if you want security. But it's fascinating.