I'm not sure whether or when it is useful (to improve performance) to dereference variables.
var x = a.b.c.d[some_key].f;
while (loop) {
do_something_with(x);
}
seems to be better than
while (loop) {
do_somthing_with(a.b.c.d[some_key].f);
}
Is that needed or is this done automagically by smart JavaScript engines?
But my actual question is whether I should do this, for example, in a library.
(function() {
var slice = Array.prototype.slice;
Function.prototype.x = function x() {
var args = slice.call(arguments, 0);
...
};
})();
or just
Function.prototype.x = function x() {
var args = Array.prototype.slice.call(arguments, 0);
...
};
The engine can't improve this automatically because it doesn't know whether Array.prototype.slice
might change during the run time.
So: does creating a closure for creating the local reference to the slice function make the script faster? Or does the additional closure scope make it slower than accessing the property "slice" of the property "prototype" of Array?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…