Vector (v3)

Revision 3 of this benchmark created on


Setup

class Vec2 {
    x;
    y;
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
    distanceTo(other) {
        return Math.sqrt((this.x - other.x)*(this.x - other.x) + (this.y - other.y)*(this.y - other.y));
    }
}

function update(bufferA, bufferB) {
    for (let x = 0;x < bufferA.length; ++x) {
        bufferB[x].x = 0;
        bufferB[x].y = 0;
        for (let y = 0;y < bufferA.length; ++y) {
            if (x === y) {
                continue;
            }
            const f = 1 / bufferA[x].distanceTo(bufferA[y]) ** 2;
            bufferB[x].x += (bufferA[x].x - bufferA[y].x) * f;
            bufferB[x].y += (bufferA[x].y - bufferA[y].y) * f;
        }
        bufferB[x].x = bufferA[x].x + bufferB[x].x;
        bufferB[x].y = bufferA[x].y + bufferB[x].y;
    }
}
function work(size) {
    const bufferA = new Array(size);
    const bufferB = Array.from(bufferA);
    for (let i = 0;i < size; ++i) {
        bufferA[i] = new Vec2(Math.random() - 0.5, Math.random() - 0.5);
        bufferB[i] = new Vec2(0, 0);
    }
    for (let iteration = 0;iteration < 128; iteration+=2) {
        update(bufferA, bufferB);
        update(bufferB, bufferA);
    }
}

Test runner

Ready to run.

Testing in
TestOps/sec
work1000
work(1000)
ready
work100
work(100)
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.