Test name | Executions per second |
---|---|
class | 1142340.9 Ops/sec |
function constructor | 1106223.8 Ops/sec |
object literal | 969796.2 Ops/sec |
object literal & sum.call | 1029964.4 Ops/sec |
__proto__ | 23787.5 Ops/sec |
Object.create | 8007.3 Ops/sec |
Object.setPrototypeOf | 5304.3 Ops/sec |
class Cls {
constructor(foo, bar, baz) {
this.foo = foo;
this.bar = bar;
this.baz = baz;
}
sum() {
return this.foo + this.bar + this.baz;
}
}
function Fn(foo, bar, baz) {
this.foo = foo;
this.bar = bar;
this.baz = baz;
}
Fn.prototype.sum = function() {
return this.foo + this.bar + this.baz;
}
function obj(foo, bar, baz) {
return {foo, bar, baz};
}
function sum(x) {
return x.foo + x.bar + x.baz;
}
function sum2() {
return this.foo + this.bar + this.baz;
}
const proto = {
sum() {
return this.foo + this.bar + this.baz;
}
}
function protoCtr(foo, bar, baz) {
return {foo, bar, baz, __proto__: proto };
}
function create(foo, bar, baz) {
let x = Object.create(proto);
x.foo = foo;
x.bar = bar;
x.baz = baz;
return x;
}
function setproto(foo, bar, baz) {
let x = {foo, bar, baz};
Object.setPrototypeOf(x, proto);
return x;
}
window.Cls = Cls
window.Fn = Fn
window.obj = obj
window.sum = sum
window.sum2 = sum2
window.protoCtr = protoCtr
window.create = create
window.setproto = setproto
const ctr = window.Cls
let i = 1000;
let y = 0;
while (i--) {
let x = new ctr(1, 2, 3);
y += x.foo + x.bar + x.baz;
y += x.sum();
}
return y;
const ctr = window.Fn;
let i = 1000;
let y = 0;
while (i--) {
let x = new ctr(1, 2, 3);
y += x.foo + x.bar + x.baz;
y += x.sum();
}
return y;
const ctr = window.obj;
const sum = window.sum;
let i = 1000;
let y = 0;
while (i--) {
let x = ctr(1, 2, 3);
y += x.foo + x.bar + x.baz;
y += sum(x);
}
return y;
const ctr = window.obj;
const sum = window.sum2;
let i = 1000;
let y = 0;
while (i--) {
let x = ctr(1, 2, 3);
y += x.foo + x.bar + x.baz;
y += sum.call(x);
}
return y;
const ctr = window.protoCtr
let i = 1000;
let y = 0;
while (i--) {
let x = ctr(1, 2, 3);
y += x.foo + x.bar + x.baz;
y += x.sum();
}
return y;
const ctr = window.create
let i = 1000;
let y = 0;
while (i--) {
let x = ctr(1, 2, 3);
y += x.foo + x.bar + x.baz;
y += x.sum();
}
return y;
const ctr = window.setproto
let i = 1000;
let y = 0;
while (i--) {
let x = ctr(1, 2, 3);
y += x.foo + x.bar + x.baz;
y += x.sum();
}
return y;