[].slice.call vs Array.prototype.slice.call
javascript, performance
Solution
Please refer to existing benchmarks
Like this one on jsperf.com I found typing "`[].slice performance" on google.
Revision 15 of the same benchmark also provides a wide variety of approaches, while revision 12 was also interesting for me.
What code to use
As @Barmar pointed out on comments, `[].slice.call` is shorter than `Array.prototype.slice.call`, so it's pretty common to see the former.
As @t.niese pointed out on comments, `[].slice.call` creates a object that is never used, even though it most likely would not have noticeable performance impacts.
IMHO, if worried about performance, I prefer creating a shortcut with `bind` on an outer scope and then use it, instead shortcuting `Array.prototype.slice.call` with `[].slice.call`:
var slice = Function.prototype.call.bind( Array.prototype.slice );
// and then just
function doSomething( ){
slice( arguments );
slice( arguments, 1, -1 );
}
slice( whatever );
slice( whatever, 0, 3 );
// and so on
Conclusions
Definitely, as seen on benchmarks, performance is not the same.
When performance really matters, just benchmark your code to optimize it according to your requeriments.
When performance does not matter enought to worry about a small improvement like this one, it's a code style related decision, so pick your personal preference or follow the code style guide of the project you are working at.
Side note
@Barmar posted a link on comments about Premature Optimization really interesting too.
Problem
I have been wondering recently. I have in the past used `[].slice.call` or `[].forEach.call` .... etc I thought doing it this way was great because it makes it easy to turn something array like, into an array easily. However, then I started thinking about it and it would be better to do something like this: `Array.prototype.slice.call` or `Array.prototype.forEach.call` instead. Am I correct in thinking that this has much better performance for the following reasons: - `[].slice.call` would create a blank array, then access the arrays prototype and would need to be garbage collected later on. - `Array.prototype.slice.call` would call the Array prototype method directly, and would not first create a blank array and then traverse the prototype tree. Is there anything I have missed? also is there anything I am missing, such as a reason why in some cases [] would be better than Array.prototype?