over these past years I was absolutely convinced (in javascript, also it should apply to any other language) that it was better, memory wise, to declare a variable and reuse it later, instead of creating a new variable every time you call a function. So now trying to win an argument I decided to write a test for this, one reuses an object (test2) and the other creates a new one upon invocation(test1):
var tmp = {x: 0};
var i = 0;
function test1(a){
return {x: a+1};
}
function test2(a){
tmp.x = a+1;
return tmp;
}
function animate() {
requestAnimationFrame( animate );
for(i=0; i< 10000000; i++)
test1(i); // or test2(i)
}
animate();
But after testing the above code, I'm getting the exact same result in the memory profiler. I've even tried using performance.memory.usedJSHeapSize (enabling the chrome flags for it to work) but I don't see any difference. So my guess is that either I'm not testing this correctly or the chrome compiler is doing some weird optimizations that avoids allocating memory in the test1.
Is there a way to know if I'm properly testing this?
Aucun commentaire:
Enregistrer un commentaire