01:49 | <Yehor Yatskevych> | * Also performance test:
const ITERATIONS_NUM = Math.pow(10, 8);
function testWithAllocation(i) {
new Vector3(i + 1.13, i + 5.231, i + 7.1247);
}
function testWithoutAllocation(i) {
if (!this.vec) {
this.vec = new Vector3(0, 0, 0);
}
this.vec.set(i + 1.13, i + 5.231, i + 7.1247);
}
console.time('with-allocation');
for (let i = 0; i < ITERATIONS_NUM; ++i) {
testWithAllocation(i);
}
console.timeEnd('with-allocation');
console.time('without-allocation');
let vec = new Vector3(0, 0, 0);
for (let i = 0; i < ITERATIONS_NUM; ++i) {
testWithoutAllocation(i);
}
console.timeEnd('without-allocation');
with-allocation: 912.862ms
without-allocation: 65.783ms
And how it looks like with static syntax to avoid unwanted allocations:
function testWithoutAllocation(i) {
static let vec = new Vector(0, 0, 0);
vec.set(i + 1.13, i + 5.231, i + 7.1247);
}
I've added answers from our discussion to the repo: https://github.com/yehoryatskevych/proposal-function-static-variables/
|