set __proto__ vs copy object
JavaScript performance comparison
Info
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="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
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) {
A.call(this, 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);
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Update __proto__ |
|
pending… |
Use Object.create and $.extend |
|
pending… |
Use constructors |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments