Skip to content

Instantly share code, notes, and snippets.

@reekoheek
Last active Sep 18, 2016
Embed
What would you like to do?
obj vs setPrototypeOf vs __proto__ vs new Obj #jsbench #jsperf (http://jsbench.github.io/#31557d49a265451ff356a56fbdfd7df5) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>obj vs setPrototypeOf vs __proto__ vs new Obj #jsbench #jsperf</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2>
</body>
</html>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
Benchmark.prototype.setup = function () {
const fooProto = {
print: function() {
return this.foo + this.bar;
},
};
const intermediateProto = {};
function mixin(proto) {
for(var i = 0; i < 10; i++) {
var x = i || '';
proto['getFoo' + x] = function() {
return this.foo;
};
proto['getBar' + x] = function() {
return this.bar;
};
}
}
mixin(intermediateProto);
function foo1() {
var x = {
foo: 'foo',
bar: 'bar',
};
mixin(x);
x.__proto__ = fooProto;
return x;
}
function foo2() {
var Foo = foo2.Foo;
if (!Foo) {
Foo = foo2.Foo = function() {
this.foo = 'foo';
this.bar = 'bar';
mixin(this);
};
Foo.prototype = fooProto;
}
return new Foo();
}
function foo3() {
var Foo = foo3.Foo;
if (!Foo) {
function BaseFoo() {
mixin(this);
}
BaseFoo.prototype = fooProto;
Foo = foo3.Foo = function() {
this.foo = 'foo';
this.bar = 'bar';
};
Foo.prototype = new BaseFoo();
}
return new Foo();
}
function foo4() {
var Foo = foo4.Foo;
if (!Foo) {
function BaseFoo() {
var keys = Object.keys(intermediateProto);
for(var i = 0; i < keys.length; i++) {
this[keys[i]] = intermediateProto[keys[i]];
}
}
BaseFoo.prototype = fooProto;
Foo = foo4.Foo = function() {
this.foo = 'foo';
this.bar = 'bar';
};
Foo.prototype = new BaseFoo();
}
return new Foo();
}
};
suite.add("var x = foo1();", function () {
var x = foo1();
(x.foo === x.getFoo());
(x.bar === x.getBar());
});
suite.add("var x = foo2();", function () {
var x = foo2();
(x.foo === x.getFoo());
(x.bar === x.getBar());
});
suite.add("var x = foo3();", function () {
var x = foo3();
(x.foo === x.getFoo());
(x.bar === x.getBar());
});
suite.add("var x = foo4();", function () {
var x = foo4();
(x.foo === x.getFoo());
(x.bar === x.getBar());
});
suite.on("cycle", function (evt) {
console.log(" - " + evt.target);
});
suite.on("complete", function (evt) {
console.log(new Array(30).join("-"));
var results = evt.currentTarget.sort(function (a, b) {
return b.hz - a.hz;
});
results.forEach(function (item) {
console.log((idx + 1) + ". " + item);
});
});
console.log("obj vs setPrototypeOf vs __proto__ vs new Obj #jsbench #jsperf");
console.log(new Array(30).join("-"));
suite.run();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment