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.callwould create a blank array, then access the arrays prototype and would need to be garbage collected later on.Array.prototype.slice.callwould 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?
[]is just because it's shorter.[].slice.callbut e.g. store theArray.prototype.slicein a localvar. The reason why I wouldn't use[].slice.callis because it creates a object that is never used, even though it most likely would not have noticeable performance impacts.Array.prototype.slice.call( arguments-or-whatever )is a common pattern to convert array-like objects (as arguments on functions), to "true" Arrays.