Maximum Set size in Node JS

TL;DR: 16,777,216

I was debugging an issue at work with the exception Caused by: RangeError: Set maximum size exceeded..I'm pretty sure I've seen this error when really the root cause was that node was out of memory, but I'm having a hard time finding evidence of that and now I'm feeling some Mandella Effect.

Chat GPT Says:

in Node.js, sets do not have a specific maximum size set by default. They can grow dynamically as you add more elements to them. However, the size of a set is ultimately constrained by the available memory of the system running the Node.js application.

If you keep adding elements to a set without any limits, it will eventually consume all available memory, potentially causing your application to slow down or crash due to memory exhaustion. It's a good practice to be mindful of memory usage and consider implementing your own logic to manage the size of the set if necessary.

If you want to impose a maximum size on a set, you would need to handle this logic yourself by removing elements when the set reaches a certain size threshold. For instance, you can periodically check the size of the set and remove elements if it exceeds a predefined limit.

But I suspect this is not actually true, because my Node Process has 12gb of memory, and I'm getting this error without exceeding 80% memory utilization. So, let's experiment:

const s = new Set();

let i = 0;
while(true) {
  process.stdOut.write(`\r${i} items`);

Running the above block will give you different results depending on your system and how much memory node is allocated by default. However, in no case will it exceed 16,777,216 items. But why? What's the significance of this number?

Well it happens to be 2^24.

Further reading of what is likely related.