Inheritance vs wrapping

JavaScript performance comparison

Test case created by matthewp

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function extendClass(Base) {
   return class extends Base {
     add(val) {
       return super.add(val) + 1;
     }
   }
  }
  
  
  let ClassBase = class {
  add(val) {
    return val + 1;
  }
  };
  
  for(let i = 0; i < 5; i++) {
    ClassBase = extendClass(ClassBase);
  }
  
  
  function extendWrap(Base) {
    let add = Base.prototype.add;
    Base.prototype.add = function(val) {
      return add.call(this, val) + 1;
    };
    return Base;
  }
  
  let WrapBase = class {
  add(val) {
    return val + 1;
  }
  };
  
  for(let i = 0; i < 5; i++) {
    WrapBase = extendWrap(WrapBase);
  }
  
  let wrapInst = new WrapBase();
  let classInst = new ClassBase();

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Inheritance
window.res = classInst.add(1);
pending…
Wrap
window.res = wrapInst.add(1);
pending…

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

Compare results of other browsers

0 Comments