Array.prototype vs [] perf

A quick question is that I really didn’t have the opportunity to peek. What is more convenient when used in a call / application context: Array.prototype vs [] ?

eg:.

 function test1() { return Array.prototype.splice.apply(arguments, [1, 2]); } test1([1,2,3,4,5,6,7,8,9]); function test2() { return [].splice.apply(arguments, [1, 2]); } test1([1,2,3,4,5,6,7,8,9]); 

My thoughts: I would suggest that the Array.prototype method Array.prototype more efficient because the prototype function can be reused and no need to create literals. Not really sure though.

Using JSPerf (with chrome), it looks like Array.prototype really a bit more efficient:

http://jsperf.com/array-perf-prototype-vs-literal

+7
source share
1 answer

It depends on the browser. In chrome, it seems .prototype is faster, firefox does not show the difference between the two, although it usually performs more slowly than chrome. IE9 shows a large speed increase for .prototype, but is the slowest browser.

However, such an optimization is so small that it can be argued that the stored time is offset by the additional bytes needed to read the code. I got distracted, although if these are the biggest performance issues you encounter, you really have no optimization problems!

EDIT:

I added an extra test here , where I used the array passed to the function to call the splice function, which turned out to be faster than in both IE, Chrome and Firefox. My conclusion is, if you already have an array, use it, otherwise use a prototype.

+3
source

All Articles