Reputation: 48406
In this post Optimization Killers, the following codes are mentioned to argument leak
.
function leaksArguments2() {
var args = [].slice.call(arguments);
}
However, I cannot understand that why arguments can leak through Array.slice
?
Upvotes: 2
Views: 694
Reputation: 2992
[].slice.call(arguments)
"leaks" arguments because it retains a reference to the arguments object. Leaking the arguments object kills optimization because it forces V8 to instantiate the arguments as a Javascript object instead of optimizing them into stack variables.
You should just be able to create a copy of the arguments array in a way that doesn't retain an object reference as mentioned in the original post:
function doesntLeakArguments() {
//.length is just an integer, this doesn't leak
//the arguments object itself
var args = new Array(arguments.length);
for(var i = 0; i < args.length; ++i) {
//i is always valid index in the arguments object
args[i] = arguments[i];
}
return args;
}
Upvotes: 2