class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
tick() {
this.x += 2;
this.y += 2;
}
}
var pointClasses = [];
var pointObjects = [];
for (let i = 0; i < 1000000; i++) {
pointClasses.push(new Point(0, 0));
pointObjects.push({ x: 0, y: 0 });
}
pointClasses.forEach((point) => {
point.tick();
});
pointObjects.forEach((point) => {
point.x += 2;
point.y += 2;
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
OO-style | |
ECS-style |
Test name | Executions per second |
---|---|
OO-style | 55.3 Ops/sec |
ECS-style | 55.5 Ops/sec |
I'll break down the benchmark and its options for you.
What is being tested?
The benchmark compares two approaches to performing an action on 1,000,000 objects:
Point
class is defined with a constructor and a tick()
method, and then instances of the class are created and used in an array.pointObjects
) and another for Point
class instances (pointClasses
). The ECS-style approach uses a method attached to an object in the same way as the OO-style approach.Options compared
The two options being compared are:
Pros and Cons of each approach:
Library and special JavaScript feature
There is no library mentioned in the benchmark definition or test cases. However, the Point
class uses a constructor function, which is a built-in JavaScript feature used for creating classes.
Test user special JS feature
There are no special JavaScript features used in this benchmark that would require specific knowledge or attention from test users.
Other alternatives
If you're interested in exploring other approaches to object-oriented programming or ECS-style systems in JavaScript, here are some alternatives: