Vector

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) ** 2 + (this.y - other.y) ** 2);
    }
}
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) {

    let bufferA = [];
    let bufferB = [];
    for (let i = 0;i < size; ++i) {
        bufferA.push(new Vec2(Math.random() - 0.5, Math.random() - 0.5));
        bufferB.push(new Vec2(0, 0));
    }
    for (let iteration = 0;iteration < 128; ++iteration) {
        update(bufferA, bufferB);
        const tmp = bufferA;
        bufferA = bufferB;
        bufferB = tmp;
    }
    console.log(bufferA[0].x);
}

Test runner

Ready to run.

Testing in
TestOps/sec
work1
work(1)
ready
work10
work(10)
ready
work100
work(100)
ready
work1000
work(1000)
ready

Revisions

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