Object.create vs Crockford vs Jorge vs Constructor vs "Safe" Constructor

JavaScript performance comparison

Revision 62 of this test case created by Riccardo Bartoli

Preparation code

<script>
    var sharedPrototype = {
        one: function() {
            return 1;
        },
        two: function() {
            return 2;
        },
        three: function() {
            return 3;
        }
    };

    var crockfordCreate = function(proto) {
        var f = function() {};
        f.prototype = proto;
        return new f();
    };

    var jorgeCreate = (function() {
        var f = function() {};
        return function(proto) {
            f.prototype = proto;
            return new f();
        };
    })();

    var Constructor = function() {};
    Constructor.prototype = sharedPrototype;

    var SafeConstructor = function() {
        if (!(this instanceof SafeConstructor)) {
            return new SafeConstructor()
        }
    };
    SafeConstructor.prototype = sharedPrototype;
</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
Object.create()
var instance = Object.create(sharedPrototype);
pending…
Crockford Create
var instance = crockfordCreate(sharedPrototype);
pending…
Jorge Create
var instance = jorgeCreate(sharedPrototype);
pending…
Constructor, using new
var instance = new Constructor();
pending…
Constructor, using new, without ()
var instance = new Constructor;
pending…
SafeConstructor, using new
var instance = new SafeConstructor();
pending…
SafeConstructor, without new
var instance = SafeConstructor();
pending…
SafeConstructor, using new, without ()
var instance = new SafeConstructor;
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.

0 Comments