set __proto__ vs copy object

JavaScript performance comparison

Test case created


When you need to change the prototype of an object that already exists, how much faster is it to use proto where supported instead of copying the object.

Preparation code

<script src="//">
Benchmark.prototype.setup = function() {
    var A = function(a1, a2) {
        this.a1 = a1;
        this.a2 = a2;
    var B = function(b1, b2) {
        this.b1 = b1;
        this.b2 = b2;
    var AA = function(a1, a2, a3) {, a1, a2);
        this.a3 = a3;
    // (new AA(1, 2, 3)) instanceof A === true
    AA.prototype = Object.create(A.prototype);
    var originalAA = new AA(1, 2, new B(new A(4, 5), new A(6, 7)));
    var serialized = JSON.stringify(originalAA);
    var deserialized = JSON.parse(serialized);

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Update __proto__
deserialized.__proto__ = AA.prototype;
deserialized.a3.__proto__ = B.prototype;
deserialized.a3.b1.__proto__ = A.prototype;
deserialized.a3.b2.__proto__ = A.prototype;
Use Object.create and $.extend
deserialized.a3.b1 = $.extend(Object.create(A.prototype), deserialized.a3.b1);
deserialized.a3.b2 = $.extend(Object.create(A.prototype), deserialized.a3.b2);
deserialized.a3 = $.extend(Object.create(B.prototype), deserialized.a3);
deserialized = $.extend(Object.create(AA.prototype), deserialized);
Use constructors
deserialized = new AA(deserialized.a1, deserialized.a2, new B(new A(deserialized.a3.b1.a1, deserialized.a3.b1.a2), new A(deserialized.a3.b2.a1, deserialized.a3.b2.a2)));

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

Compare results of other browsers


Add a comment