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;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
class | |
function constructor | |
object literal | |
object literal & sum.call | |
__proto__ | |
Object.create | |
Object.setPrototypeOf |
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 |
A behemoth of a benchmark report!
After analyzing the data, I'll try to summarize the results for each test:
1. class: The class
constructor is the fastest among all the tests, with an execution rate of 1791449.875 executions per second.
2. function constructor: The function
constructor comes in second, with a slightly lower execution rate of 1778884.875 executions per second.
3. object literal & sum.call: This test uses a function call to calculate the sum, which results in a slower execution rate of 1651123.875 executions per second.
4. object literal: This test uses an object literal to create objects, with an execution rate of 1591565.875 executions per second.
5. proto: The __proto__
benchmark is the slowest, with a negligible execution rate of 41941.46484375 executions per second.
6. Object.create: This test uses the Object.create
method to create objects, resulting in an even slower execution rate of 17102.509765625 executions per second.
7. Object.setPrototypeOf: The last benchmark is also slow, with a relatively low execution rate of 11653.7314453125 executions per second.
Overall, it's clear that the class
constructor is the most efficient way to create objects in JavaScript, followed closely by the function constructor.