Backbone Vs John Resig's Simple JavaScript Inheritance Vs. Crockford Prototypal

JavaScript performance comparison

Revision 2 of this test case created

Info

This is a performance test of object creation between Backbone models, John Resig's Simple JavaScript Inheritance script, and Crockford Prototypal. (prototypal inheritance, being a different type of inheritance does not bring the instanceof/class inheritance so this needs to be taken into consideration)

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js">
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js">
</script>

<script type="text/javascript">
//Crockford near evolved
Object.doCreate = function () {
    function F() {}
    F.prototype = this;
    return new F();
};
</script>
<script type="text/javascript">
  /* Simple JavaScript Inheritance
 * By John Resig http://ejohn.org/
 * MIT Licensed.
 */

  // Inspired by base2 and Prototype
  (function() {
    var a = false,
        b = /xyz/.test(function() {
        xyz
      }) ? /\b_super\b/ : /.*/;
    this.Class = function() {};
    Class.extend = function(g) {
      var f = this.prototype;
      a = true;
      var e = new this();
      a = false;
      for (var d in g) {
        e[d] = typeof g[d] == "function" && typeof f[d] == "function" && b.test(g[d]) ? (function(h, i) {
          return function() {
            var k = this._super;
            this._super = f[h];
            var j = i.apply(this, arguments);
            this._super = k;
            return j
          }
        })(d, g[d]) : g[d]
      }
      function c() {
        if (!a && this.init) {
          this.init.apply(this, arguments)
        }
      }
      c.prototype = e;
      c.prototype.constructor = c;
      c.extend = arguments.callee;
      return c
    }
  })();


</script>
<script>
Benchmark.prototype.setup = function() {
    window.array = [];
   
    myModel = Backbone.Model.extend({
      defaults: {
        foo: "bar",
        fizz: "buzz",
        a: 1,
        b: 2
      }
    });
   
    ResigsClass = Class.extend({
   
      foo: "bar",
      fizz: "buzz",
      a: 1,
      b: 2
   
    });
   
    CrockfordClass = {
   
      foo: "bar",
      fizz: "buzz",
      a: 1,
      b: 2
   
    };
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Create Backbone.Model objects
array.push(Backbone.Model.extend({
  defaults: {
    foo: "bar",
    fizz: "buzz",
    a: 1,
    b: 2
  }
}));
pending…
Create new instance of Backbone.Model
array.push(new myModel());
pending…
John Resig Approach
array.push(new ResigsClass());
pending…
Crockford approach
array.push(Object.doCreate(CrockfordClass));
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment