title: How to force JavaScript to deep copy a string?
body: <p>I have some javascript code which looks like this:</p>
  
  <pre><code>var myClass = {
    ids: {}
    myFunc: function(huge_string) {
       var id = huge_string.substr(0,2);
       ids[id] = true;
    }
  }
  </code></pre>
  
  <p>Later the function gets called with some large strings (100 MB+). I only want to save a short id which I find in each string. However, the Google Chrome's substring function (actually regex in my code) only returns a "sliced string" object, which references the original. So after a series of calls to <code>myFunc</code>, my chrome tab runs out of memory because the temporary <code>huge_string</code> objects are not able to be garbage collected.</p>
  
  <p>How can I make a copy of the string <code>id</code> so that a reference to the <code>huge_string</code> is not maintained, and the <code>huge_string</code> can be garbage collected?</p>
  
  <p><a href="https://i.stack.imgur.com/pzJai.png" rel="noreferrer"><img src="https://i.stack.imgur.com/pzJai.png" alt="enter image description here"></a></p>