// classes declared here can't be accessed in test cases for some reason...
class X {
constructor(foo, bar, baz) {
this.foo = foo;
this.bar = bar;
this.baz = baz;
}
sum() {
return this.foo + this.bar + this.baz;
}
}
let i = 1000;
let y = 0;
while (i--) {
let x = new X(1, 2, 3);
y += x.foo + x.bar + x.baz;
y += x.sum();
}
return y;
class X {
foo;
bar;
baz;
constructor(foo, bar, baz) {
this.foo = foo;
this.bar = bar;
this.baz = baz;
}
sum() {
return this.foo + this.bar + this.baz;
}
}
let i = 1000;
let y = 0;
while (i--) {
let x = new X(1, 2, 3);
y += x.foo + x.bar + x.baz;
y += x.sum();
}
return y;
function X(foo, bar, baz) {
this.foo = foo;
this.bar = bar;
this.baz = baz;
}
X.prototype.sum = function() {
return this.foo + this.bar + this.baz;
}
let i = 1000;
let y = 0;
while (i--) {
let x = new X(1, 2, 3);
y += x.foo + x.bar + x.baz;
y += x.sum();
}
return y;
function X(foo, bar, baz) {
return {foo, bar, baz};
}
function sum(x) {
return x.foo + x.bar + x.baz;
}
let i = 1000;
let y = 0;
while (i--) {
let x = {foo: 1, bar: 2, baz: 3};
y += x.foo + x.bar + x.baz;
y += sum(x);
}
return y;
function X(foo, bar, baz) {
return {foo, bar, baz};
}
function sum() {
return this.foo + this.bar + this.baz;
}
let i = 1000;
let y = 0;
while (i--) {
let x = X(1, 2, 3);
y += x.foo + x.bar + x.baz;
y += sum.call(x);
}
return y;
function X(foo, bar, baz) {
let x = {foo, bar, baz};
x.sum = sum.bind(x);
return x;
}
function sum() {
return this.foo + this.bar + this.baz;
}
let i = 1000;
let y = 0;
while (i--) {
let x = X(1, 2, 3);
y += x.foo + x.bar + x.baz;
y += x.sum();
}
return y;
function X(foo, bar, baz) {
let x = Object.create(proto);
x.foo = foo;
x.bar = bar;
x.baz = baz;
return x;
}
const proto = {
sum() {
return this.foo + this.bar + this.baz;
}
}
let i = 1000;
let y = 0;
while (i--) {
let x = X(1, 2, 3);
y += x.foo + x.bar + x.baz;
y += x.sum();
}
return y;
function X(foo, bar, baz) {
let x = {foo, bar, baz};
Object.setPrototypeOf(x, proto);
return x;
}
const proto = {
sum() {
return this.foo + this.bar + this.baz;
}
}
let i = 1000;
let y = 0;
while (i--) {
let x = X(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 without declared fields | |
class with declared fields | |
function constructor | |
object literal | |
object literal & sum.call | |
object literal & sum.bind | |
Object.create | |
Object.setPrototypeOf |
Test name | Executions per second |
---|---|
class without declared fields | 63465.6 Ops/sec |
class with declared fields | 81251.4 Ops/sec |
function constructor | 67440.1 Ops/sec |
object literal | 1583286.2 Ops/sec |
object literal & sum.call | 1613394.8 Ops/sec |
object literal & sum.bind | 91517.2 Ops/sec |
Object.create | 47204.8 Ops/sec |
Object.setPrototypeOf | 43916.7 Ops/sec |
It looks like we have a benchmarking result for JavaScript performance comparisons!
After analyzing the results, I'll provide some insights:
let x = { foo: 'bar', bar: 'baz' };
) rather than other methods.sum()
function call improves performance.These results may be influenced by factors such as:
X(1, 2, 3)
Keep in mind that these findings are based on a single benchmarking result and should be interpreted with caution.