title: Maximum number of entries in Node.js Map?
body: <p>I was making a large <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" rel="noreferrer"><code>Map</code></a> in Node.js v11.9.0 and it kept failing with "FATAL ERROR: invalid table size Allocation failed - JavaScript heap out of memory". My map's keys and values shouldn't be getting anywhere near the size of Node's heap size, so I tried just making a map and inserting numeric keys and values into it:</p>
  
  <pre><code>var N = Math.pow(2, 26);
  var map = new Map();
  for (var i = 0; i &lt; N; i++) {
    map.set(i, i + 1);
    if (i % 1e5 === 0) { console.log(i / 1e6); }
  }
  </code></pre>
  
  <p><em>This program crashes Node after inserting roughly 16.6 million entries.</em> That number seemed suspiciously close to 2^24, so replacing the logging above with <code>if (i &gt; 16777200) { console.log(i); }</code>, I see that the program crashes immediately after successfully printing "16777215", which is one less than 2^24.</p>
  
  <p>Question. <strong>Is there a documented limit on the number of entries in Node's <code>Map</code> close to 2^24? Is there any way to raise that limit?</strong></p>
  
  <p>(N.B. Running Node as <code>node --max-old-space-size=4096</code> doesn't prevent the crash, since Node is using far less than 4 GB RAM.)</p>
  
  <p>(N.B. 2. I don't think this is a hash collision issue since in my actual code, the map contains (short-ish) strings rather than numbers.)</p>
  
  <p>(N.B. 3. Running the above programs in Firefox's JavaScript Console does not kill Firefox–Firefox keeps adding entries well past 30 million. However, Chrome crashes just like Node. So this is likely a V8 limitation.)</p>