Skip to content

Instantly share code, notes, and snippets.

@reekoheek
Last active Sep 16, 2016
Embed
What would you like to do?
new vs mixin vs object.create #jsbench #jsperf (http://jsbench.github.io/#1495d021336579c3e8f5b7dc83fd7dbe) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>new vs mixin vs object.create #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 () {
var fooProto = {
_foo: 'foo',
_getFoo: function() {
return this._foo;
}
};
function Foo1() {
this._foo = 'foo1';
this._bar = 'bar';
}
Foo1.prototype = fooProto
function foo2() {
var foo = {
_foo: 'foo2',
_bar: 'bar',
};
Object.setPrototypeOf(foo, fooProto);
return foo;
}
function foo3() {
return Object.create(fooProto, {
_foo: {
value: 'foo3',
},
_bar: {
value: 'bar',
},
});
}
function foo4() {
var foo = Object.create(fooProto);
foo._foo = 'foo4';
foo._bar = 'bar';
return foo;
}
function foo5() {
var foo = {
_foo: 'foo5',
_bar: 'bar',
};
foo.__proto__ = fooProto;
return foo;
}
function FooProto() {}
FooProto.prototype = fooProto;
class Foo6 extends FooProto {
constructor() {
super();
this._foo = 'foo6';
this._bar = 'bar';
}
}
};
suite.add("for(var i = 0; i < 100; i++) {", function () {
for(var i = 0; i < 100; i++) {
var x = new Foo1();
var foo = x._foo;
var bar = x._bar;
var getfoo = x._getFoo();
}
});
suite.add("for(var i = 0; i < 100; i++) {", function () {
for(var i = 0; i < 100; i++) {
var x = foo2();
var foo = x._foo;
var bar = x._bar;
var getfoo = x._getFoo();
}
});
suite.add("for(var i = 0; i < 100; i++) {", function () {
for(var i = 0; i < 100; i++) {
var x = foo3();
var foo = x._foo;
var bar = x._bar;
var getfoo = x._getFoo();
}
});
suite.add("for(var i = 0; i < 100; i++) {", function () {
for(var i = 0; i < 100; i++) {
var x = foo4();
var foo = x._foo;
var bar = x._bar;
var getfoo = x._getFoo();
}
});
suite.add("for(var i = 0; i < 100; i++) {", function () {
for(var i = 0; i < 100; i++) {
var x = foo5();
var foo = x._foo;
var bar = x._bar;
var getfoo = x._getFoo();
}
});
suite.add("for(var i = 0; i < 100; i++) {", function () {
for(var i = 0; i < 100; i++) {
var x = new Foo6();
var foo = x._foo;
var bar = x._bar;
var getfoo = x._getFoo();
}
});
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("new vs mixin vs object.create #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