class-perfs (v12)

Revision 12 of this benchmark created by crossjs on


Preparation HTML

<script src='http://documentcloud.github.com/underscore/underscore.js'></script>
<script src='http://documentcloud.github.com/backbone/backbone-min.js'></script>
<script>
(function(module){
'use strict';
var inherit = function(Child, Parent) {
  var Bridge = function() {};
  Bridge.prototype = Parent.prototype;
  Child.prototype = new Bridge();
  Child.superclass = Parent.prototype;
  Child.prototype.constructor = Child;
};

var slice = Array.prototype.slice,
  unshift = Array.prototype.unshift;

var mixin = function() {
  var args = slice.call(arguments, 0),
    target = args.shift(),
    source, p;

  while ((source = args.shift())) {
    for (p in source) {
      // 保留属性 mixins
      if (p === 'mixins') {
        source[p].unshift(target);
        mixin.apply(null, source[p]);
      } else {
        target[p] = source[p];
      }
    }
  }
};
var Class = function() {};

Class.superclass = Class.prototype = {

  // constructor: Class,

  /**
   * 初始化方法,实例化时自动执行
   *
   * @method initialize
   */
  initialize: function() {},
  extend: function( /*[properties[, ... properties]]*/ ) {
    Array.prototype.unshift.call(arguments, this);

    mixin.apply(null, arguments);
    return this;
  }

};

Class.create = function( /*[Parent][, properties[, ... properties]]*/ ) {

  var args = slice.call(arguments, 0),
    Dummy,
    Parent;

  Dummy = function() {
    this.initialize.apply(this, arguments);
  };

  if (args[0] && typeof args[0] === 'function') {
    Parent = args.shift();
    // 确保继承自 Class 或 Class 的子类
    if (!Parent.superclass) {
      inherit(Parent, Class);
    }
  } else {
    Parent = Class;
  }

  inherit(Dummy, Parent);

  if (args.length) {
    args.unshift(Dummy.prototype);
    mixin.apply(null, args);
  }
  Dummy.extend = function( /*[properties[, ... properties]]*/ ) {
    unshift.call(arguments, Dummy);
    return Class.create.apply(null, arguments);
  };

  return Dummy;
};

module.CrossClass= Class;

})(window);
</script>
<script src="http://libs.baidu.com/mootools/1.4.5/mootools-yui-compressed.js"></script>

Test runner

Ready to run.

Testing in
TestOps/sec
backbone
var Animal = Backbone.Model.extend({
  initialize: function(name) {
    this.name = name;
  },
  talk: function() {
    return 'I am ' + this.name;
  }
});

var Bird = Animal.extend({
  initialize: function(name) {
    Animal.prototype.initialize.call(this, name);
  },
  fly: function() {
    return 'I am flying';
  }
});

new Animal();
var bird = new Bird();
bird.talk();
bird.fly();
ready
mootools
var Animal = new Class({
  initialize: function(name) {
    this.name = name;
  },
  talk: function() {
    return 'I am ' + this.name;
  }
});

var Bird = new Class({
  Extends: Animal,
  initialize: function(name) {
    this.parent(name);
  },
  fly: function() {
    return 'I am flying';
  }
});

new Animal();
var bird = new Bird();
bird.talk();
bird.fly();
ready
coffeescript
// Generated by CoffeeScript 1.6.2

// class Animal
//   constructor: (@name) ->
//   talk: ->
//     "I am #{@name}"

// class Bird extends Animal
//   fly: ->
//     "I am flying"

// new Animal
// bird = new Bird
// bird.talk()
// bird.fly()

(function() {
  var Animal, Bird, bird, _ref,
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  Animal = (function() {
    function Animal(name) {
      this.name = name;
    }

    Animal.prototype.talk = function() {
      return "I am " + this.name;
    };

    return Animal;

  })();

  Bird = (function(_super) {
    __extends(Bird, _super);

    function Bird() {
      _ref = Bird.__super__.constructor.apply(this, arguments);
      return _ref;
    }

    Bird.prototype.fly = function() {
      return "I am flying";
    };

    return Bird;

  })(Animal);

  new Animal;

  bird = new Bird;

  bird.talk();

  bird.fly();

}).call(this);
ready
crossjs
var Animal = CrossClass.create({
  initialize: function(name) {
    this.name = name;
  },
  talk: function() {
    return 'I am ' + this.name;
  }
});

var Bird = Animal.extend({
  initialize: function(name) {
    Bird.superclass.initialize.call(this, name);
  },
  fly: function() {
    return 'I am flying';
  }
});

new Animal();
var bird = new Bird();
bird.talk();
bird.fly();
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.