Inheritance {} vs new Object()

JavaScript performance comparison

Revision 3 of this test case created by valmynd

Info

Setup Speed

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    // 1
   
    function MyClass1() {}
   
    MyClass1.prototype = {}
    MyClass1.prototype.constructor = MyClass1
   
    // 2
   
    function MyClass2() {}
   
    MyClass2.prototype = new Object()
    MyClass2.prototype.constructor = MyClass2
   
    // 3
   
    MyClass3 = new Function()
   
    MyClass3.prototype = new Object()
    MyClass3.prototype.constructor = MyClass3
   
    // 4
   
    /**
     * 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 MyClass4 = Class(Object, function() {
    })
   
    // 5
   
    /**
     * Author C. Wilhelm https://github.com/valmynd/
     * License: The MIT License http://opensource.org/licenses/MIT
     */

    function Class2(baseclass, setupfunc) {
      if(baseclass === Object) {
       var c = function () {}, prototype = {}
      } else {
        var c = function () {}, prototype = new baseclass()
      }
        prototype.constructor = c
        setupfunc.apply(prototype)
        prototype.constructor.prototype = prototype
        return prototype.constructor
    }
   
    var MyClass5 = 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
inheritt from {}
function MyClass1() {
}

MyClass1.prototype = {}
MyClass1.prototype.constructor = MyClass1

var obj = new MyClass1()
pending…
inheritt from new Object()
function MyClass2() {
}

MyClass2.prototype = new Object()
MyClass2.prototype.constructor = MyClass2

var obj = new MyClass2()
pending…
new Function inheriting from new Object()
MyClass3 = new Function()

MyClass3.prototype = new Object()
MyClass3.prototype.constructor = MyClass3

var obj = new MyClass3()
pending…
inheritance using Class Utility 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 MyClass4 = Class(Object, function () {
})

var obj = new MyClass4()
pending…
inherit from {} using modified Class Utility function
/**
 * Author C. Wilhelm https://github.com/valmynd/
 * License: The MIT License http://opensource.org/licenses/MIT
 */

function Class2(baseclass, setupfunc) {
        if (baseclass === Object) {
                var c = function () {
                }, prototype = {}
        } else {
                var c = function () {
                }, prototype = new baseclass()
        }
        prototype.constructor = c
        setupfunc.apply(prototype)
        prototype.constructor.prototype = prototype
        return prototype.constructor
}

var MyClass5 = Class2(Object, function () {
})

var obj = new MyClass5()
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