Inheritance {} vs new Object()

JavaScript performance comparison

Revision 2 of this test case created by valmynd

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    /**
     * Author C. Wilhelm https://github.com/valmynd/
     * License: The MIT License http://opensource.org/licenses/MIT
     */

    function Class(baseclass, setupfunc) {
      var c = function() {}, prototype = new baseclass()
        prototype.constructor = c
        setupfunc.apply(prototype)
        prototype.constructor.prototype = prototype
      return prototype.constructor
    }
   
    var MyClass1 = Class(Object, function() {})
   
   
    /**
     * Author C. Wilhelm https://github.com/valmynd/
     * License: The MIT License http://opensource.org/licenses/MIT
     */

    function Class2(baseclass, setupfunc) {
      if (!baseclass || baseclass === Object) {
        var c = function() {}
        setupfunc.apply(c.prototype)
        return c
      } else {
        var c = function() {}, prototype = new baseclass()
          prototype.constructor = c
          setupfunc.apply(prototype)
          prototype.constructor.prototype = prototype
        return prototype.constructor
      }
    }
   
    var MyClass2 = Class2(Object, function() {})
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
inheritance using Class Utility function
var obj = new MyClass1()
pending…
inheritance using a modified Class Utility function
var obj = new MyClass2()
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