Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Chrome 134
Windows
Desktop
9 days ago
Test name Executions per second
SoA 3521552.0 Ops/sec
AoS 3509384.0 Ops/sec
Script Preparation code:
x
 
'use strict';
window.N = 1000000;
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|0, Math.random() * 100|0, Math.random() * 100|0, Math.random() * 100|0));
// 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));
Tests:
  • SoA

     
    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;
    }
  • AoS

     
    var N = window.N;
    for (var i = 0; i < N; ++i) {
        var pav = aos[i]
        pav.x += pav.vx;
        pav.y += pav.vy;
    }