Will javascript private members in classes cause a huge memory overhead?
<p>In JavaScript, fields of an object are always "public":</p>

<pre><code>function Test() {
  this.x_ = 15;
}
Test.prototype = {
  getPublicX: function() {
    return this.x_;
  }
};
new Test().getPublicX();       // using the getter
new Test().x_;                 // bypassing the getter
</code></pre>

<p>but you can simulate a "private" field by using a local variable, and using a closure as the getter:</p>

<pre><code>function Test() {
  var x = 15;
  this.getPrivateX = function() {
    return x;
  };
}
new Test().getPrivateX();       // using the getter
// ... no way to access x directly: it's a local variable out of scope
</code></pre>

<p>One difference is that with the "public" approach, each instance's getter is the same function object:</p>

<pre><code>console.assert(t1.getPublicX === t2.getPublicX);
</code></pre>

<p>whereas with the "private" approach, each instance's getter is a distinct function object:</p>

<pre><code>console.assert(t1.getPrivateX != t2.getPrivateX);
</code></pre>

<p>I'm curious about the memory usage of this approach. Since each instance has a separate <code>getPrivateX</code>, will this cause a huge memory overhead if I create, say, 10k instances?</p>

<p>A performance test on creating instances of classes with private and public members:</p>

<p><a href="http://jsperf.com/private-public-member" rel="nofollow">Jsperf</a></p>
