JS Inheritance Performance (v47)

Revision 47 of this benchmark created on


Description

Testing widely used inheritance libraries against Fiber.js and native javascript

Preparation HTML

<script>  
this.Tk = {
  compose: function(base, definition) {
    if (arguments.length < 2) {
      definition = base;
      base = {};
    } else {
       base = base.__base__;
    }
    definition = definition(base);
    var properties = Object.keys(base);
    var i = 0;
    var property;
    var length = properties.length;
    for (; i < length; i++) {
      property = properties[i];
      if (definition.hasOwnProperty(property) === true) {
        continue;
      }
      definition[property] = base[property];
    }
    function Struct() {
       var instance = Object.create(definition);
       if (definition.hasOwnProperty('init')) {
         instance.init.apply(null, arguments);    
       }
       return instance;
    }
    Struct.__base__ = definition;

    return Struct;
  }
};
</script>
<script src="http://kiro.me/temp/fiber.js"></script>

Test runner

Ready to run.

Testing in
TestOps/sec
Fiber.js
var A = Fiber.extend(function() {
  return {
    init: function(val) {
      this.val = val;
    },
    method1: function(x, y, z) {
      this.x = x;
      this.y = y;
      this.z = z;
    }
  }
});

var B = A.extend(function(base) {
  return {
    method1: function(y, z) {
      base.method1.call(this, 'x', y, z);
    }
  }
});

var C = B.extend(function(base) {
  return {
    method1: function(z) {
      base.method1.call(this, 'y', z);
    }
  }
});

var a = new A("a");
a.method1("x", "y", "z");

var b = new B("b");
b.method1("y", "z");

var c = new C("c");
c.method1("z");
 
ready
Fiber.js (rewrite)
var A = Tk.compose(function() {
  return {
    init: function(val) {
      this.val = val;
    },
    method1: function(x, y, z) {
      this.x = x;
      this.y = y;
      this.z = z;
    }
  }
});

var B = Tk.compose(A, function(base) {
  return {
    method1: function(y, z) {
      base.method1.call(this, 'x', y, z);
    }
  }
});

var C = Tk.compose(B, function(base) {
  return {
    method1: function(z) {
      base.method1.call(this, 'y', z);
    }
  }
});

var a = A("a");
a.method1("x", "y", "z");

var b = B("b");
b.method1("y", "z");

var c = C("c");
c.method1("z");
 
ready

Revisions

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