Test name | Executions per second |
---|---|
OOP | 9.0 Ops/sec |
Fake OOP2 | 13.5 Ops/sec |
Fake OOP3 | 8.8 Ops/sec |
FP | 30.8 Ops/sec |
window.iterations = 100000;
class Obj {
constructor(x, y, z) {
this.update(x, y, z);
}
update(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
this.result = x + y + z;
}
}
const OOP = [];
for (let i = 0; i < iterations; i++ ) {
OOP.push(new Obj(Math.random(), Math.random(), Math.random()));
};
for (let i = 0; i < iterations; i++ ) {
OOP[i].update(Math.random(), Math.random(), Math.random());
};
function createObj(x, y, z) {
return {
x,
y,
z,
result: x + y + z
}
}
function updateObj(obj, x, y, z) {
obj.x = x;
obj.y = y;
obj.z = z;
obj.result = x + y + z;
}
class Obj2 {
constructor(x, y, z) {
this.obj = createObj(x, y, z);
}
update(x, y, z) {
updateObj(this.obj, x, y, z);
}
}
const FakeOOP2 = [];
for (let i = 0; i < iterations; i++ ) {
FakeOOP2.push(new Obj2(Math.random(), Math.random(), Math.random()));
};
for (let i = 0; i < iterations; i++ ) {
FakeOOP2[i].update(Math.random(), Math.random(), Math.random());
};
function createObj(x, y, z) {
return {
x,
y,
z,
result: x + y + z
}
}
function updateObj(obj, x, y, z) {
obj.x = x;
obj.y = y;
obj.z = z;
obj.result = x + y + z;
}
class Obj3 {
constructor(x, y, z) {
Object.assign(this, createObj(x, y, z));
}
update(x, y, z) {
updateObj(this, x, y, z);
}
}
const FakeOOP3 = [];
for (let i = 0; i < iterations; i++ ) {
FakeOOP3.push(new Obj3(Math.random(), Math.random(), Math.random()));
};
for (let i = 0; i < iterations; i++ ) {
FakeOOP3[i].update(Math.random(), Math.random(), Math.random());
};
function createObj(x, y, z) {
return {
x,
y,
z,
result: x + y + z
}
}
function updateObj(obj, x, y, z) {
obj.x = x;
obj.y = y;
obj.z = z;
obj.result = x + y + z;
}
const FP = [];
for (let i = 0; i < iterations; i++ ) {
FP.push(createObj(Math.random(), Math.random(), Math.random()));
};
for (let i = 0; i < iterations; i++ ) {
updateObj(FP[i], Math.random(), Math.random(), Math.random());
};