'use strict';
window.N = Math.pow(10, 9);
class PointAndVelocity {
constructor(x, y, vx, vy) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
}
}
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
class Velocity {
constructor(vx, vy) {
this.vx = vx;
this.vy = vy;
}
}
aos = Array(N).fill(new PointAndVelocity(0, 0, 0, 0))
.map(() => new PointAndVelocity(Math.random() * 100, Math.random() * 100, Math.random() * 100, Math.random() * 100));
// SoA, we try to duplicate the data so numeric differences are out of the question.
points = Array(N).fill(new Point(0, 0)).
.map((_, i) => new Point(aos[i].x, aos[i].y));
velocities = Array(N).fill(new Velocity(0, 0)).
.map((_, i) => new Velocity(aos[i].vx, aos[i].vy));
var N = window.N;
for (var i = 0; i < N; ++i) {
var point = points[i];
var velocity = velocities[i];
point.x += velocity.vx;
point.y += velocity.vy;
}
var N = window.N;
for (var i = 0; i < N; ++i) {
var pav = aos[i]
pav.x += pav.vx;
pav.y += pav.vy;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
SoA | |
AoS |
Test name | Executions per second |
---|---|
SoA | 3472144.0 Ops/sec |
AoS | 3482580.8 Ops/sec |
The benchmark titled "Classes SoA vs AoS" compares two data organization strategies: Structure of Arrays (SoA) and Array of Structures (AoS). These approaches are being evaluated in the context of JavaScript programming, specifically looking at the performance of operations involving class instances.
Classes and Objects: The test employs three classes:
PointAndVelocity
: Represents both position (x, y) and velocity (vx, vy) in a single structure.Point
: Represents position only.Velocity
: Represents velocity only.Data Population: The benchmark initializes two datasets:
aos
) consists of PointAndVelocity
objects. This simulates a traditional approach where related data is grouped together in a single structure.points
and velocities
) represent the same data but separated into two distinct structures: one for positions and one for velocities. This format is commonly advantageous in data processing for performance optimization.AoS Test Case:
aos
array, directly updating the position (x, y) of each PointAndVelocity
object based on its velocity (vx, vy).SoA Test Case:
points
and velocities
arrays independently, updating positions based on the respective velocity arrays.The benchmark results show that the AoS approach (3,482,580.75 executions per second) outperformed the SoA approach (3,472,144.0 executions per second). Several factors could influence these outcomes:
Other data structures and approaches exist that can cater to various needs:
In conclusion, this benchmark provides a meaningful insight into the performance implications of using classes and object-oriented design principles in JavaScript, exemplified by the traditional versus contemporary data organization techniques. Understanding the trade-offs between SoA and AoS can aid engineers in making informed decisions, especially when performance is critical.