Suppose I have a function that I need to call a lot, maybe a few thousand times on every mouse down or mouse move. It uses an instance of a function (class), called Transform
:
function func1(a, b, c) {
var t = new Transform();
t.rotate(a);
t.scale(b, c);
return t.m[0];
}
So I'm creating thousands of new transforms as I call this func1
lots.
What if, instead of creating new Transform()
s every time, I created a small system to allocate extra transforms only as they are needed, and re-use them:
window.Util = {
_CachedTransforms: [],
tempTransform: function() {
var arr = Util._CachedTransforms;
var temp = arr.pop();
if (temp === undefined) return new Transform();
return temp;
},
freeTransform: function(temp) {
Util._CachedTransforms.push(temp);
}
}
Then instead I could call func2
:
function func2(a, b, c) {
var t = Util.tempTransform();
t.reset();
t.rotate(a);
t.scale(b, c);
var result = t.m[0];
Util.freeTransform(t);
return result;
}
Using func2
several thousand times, new Transform
is only ever called once. This might suggest a benefit, but the numbers from jsperf don't seem to suggest any.
If you want to see these two functions in action as well as the Transform class, take a look at jsperf: http://jsperf.com/transforms
And especially: http://jsperf.com/transforms/2
To simulate it occuring lots during an event, my jsperf test does:
var a;
for (var i = 0; i < 4000; i++) {
a += func1(1, i, 3); // vs func2
}
There may be better ways to test if this is advantageous or not. Am I missing something?
More broadly, is object reuse like this still a good idea in this scenario, or ever?
Best Answer
This is a method known as "object pooling", and you are correct that it can speed up performance. Object creation is usually a fairly inexpensive task in Javascript (though
Object.create
can be pretty slow surprisingly in my experience), but needing to invoke the GC repeatedly can cause noticeable performance issues.Of course, you should only optimize if you have figures that suggest you have to.
I'm kind of on the same page as Avner Shahar-Kashtan in that object creation might not even be required in this case.