title: What makes my.class.js so fast?
body: <p>I've been looking at the source code of <a href="https://github.com/jiem/my-class/blob/master/my.class.js" rel="nofollow">my.class.js</a> to find out what makes it so <a href="http://jsperf.com/oop-benchmark/107" rel="nofollow" title="JavaScript Object Oriented Libraries Benchmark · jsPerf">fast</a> on Firefox. Here's the snippet of code used to create a class:</p>
  
  <pre><code>my.Class = function () {
      var len = arguments.length;
      var body = arguments[len - 1];
      var SuperClass = len &gt; 1 ? arguments[0] : null;
      var hasImplementClasses = len &gt; 2;
      var Class, SuperClassEmpty;
  
      if (body.constructor === Object) {
          Class = function () {};
      } else {
          Class = body.constructor;
          delete body.constructor;
      }
  
      if (SuperClass) {
          SuperClassEmpty = function() {};
          SuperClassEmpty.prototype = SuperClass.prototype;
          Class.prototype = new SuperClassEmpty();
          Class.prototype.constructor = Class;
          Class.Super = SuperClass;
          extend(Class, SuperClass, false);
      }
  
      if (hasImplementClasses)
          for (var i = 1; i &lt; len - 1; i++)
              extend(Class.prototype, arguments[i].prototype, false);    
  
      extendClass(Class, body);
  
      return Class;
  };
  </code></pre>
  
  <p>The <code>extend</code> function is simply used to copy the properties of the second object onto the first (optionally overriding existing properties):</p>
  
  <pre><code>var extend = function (obj, extension, override) {
      var prop;
      if (override === false) {
          for (prop in extension)
              if (!(prop in obj))
                  obj[prop] = extension[prop];
      } else {
          for (prop in extension)
              obj[prop] = extension[prop];
          if (extension.toString !== Object.prototype.toString)
              obj.toString = extension.toString;
      }
  };
  </code></pre>
  
  <p>The <code>extendClass</code> function copies all the static properties onto the class, as well as all the public properties onto the prototype of the class:</p>
  
  <pre><code>var extendClass = my.extendClass = function (Class, extension, override) {
      if (extension.STATIC) {
          extend(Class, extension.STATIC, override);
          delete extension.STATIC;
      }
      extend(Class.prototype, extension, override);
  };
  </code></pre>
  
  <p>This is all pretty straightforward. When you create a class, it simply returns the constructor function you provide it.</p>
  
  <p>What beats my understanding however is how does creating an instance of this constructor <a href="http://jsperf.com/oop-benchmark/107" rel="nofollow" title="JavaScript Object Oriented Libraries Benchmark · jsPerf">execute faster</a> than creating an instance of the same constructor written in <a href="http://vaporjs.com/" rel="nofollow" title="Vapor.js | The World's Smallest &amp; Fastest JavaScript Library">Vapor.js</a>.</p>
  
  <p>This is what I'm trying to understand:</p>
  
  <ol>
  <li>How do constructors of libraries like my.class.js create so many instances so quickly on Firefox? The constructors of the libraries are all very similar. Shouldn't the execution time also be similar?</li>
  <li>Why does the way the class is created affect the execution speed of instantiation? Aren't definition and instantiation separate processes?</li>
  <li>Where is my.class.js gaining this speed boost from? I don't see any part of the constructor code which should make it execute any faster. In fact traversing a long prototype chain like <code>MyFrenchGuy.Super.prototype.setAddress.call</code> should slow it down significantly.</li>
  <li>Is the constructor function being JIT compiled? If so then why aren't the constructor functions of other libraries also being JIT compiled?</li>
  </ol>