简体   繁体   English

Array.prototype vs [] perf

[英]Array.prototype vs [] perf

Quick question that I haven't really had a chance to look into. 我还没有真正有机会研究的快速问题。 Which is more performant when used in a call/apply sort of context: Array.prototype vs [] ? 在调用/应用上下文中使用时哪个更Array.prototypeArray.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 assume the Array.prototype way is more performant because a prototype function can be reused and no literal need be created. 我的想法:我认为Array.prototype方式更具性能,因为原型函数可以重用,不需要创建文字。 Not really sure though. 虽然不太确定。

Using JSPerf (with chrome) it looks like the Array.prototype is indeed slightly more performant: 使用JSPerf(带有chrome),看起来Array.prototype性能确实稍高:

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

It depends on the browser running it. 这取决于运行它的浏览器。 In chrome it seems .prototype is faster, firefox shows no difference between the two although generally performs slower than chrome. 在chrome中,似乎.prototype更快,firefox显示两者之间没有差异,虽然通常比chrome更慢。 IE9 shows a big speed increase for .prototype but is the slowest browser by far. IE9显示.prototype的速度大幅增加,但到目前为止是最慢的浏览器。

However, this sort of optimization is so small that one could argue the time saved is offset against the extra bytes required to read the code. 但是,这种优化是如此之小以至于人们可以争辩说节省的时间会与读取代码所需的额外字节相抵消。 I digress though, If these are the biggest performance issues your coming against then you really don't have any problems with optimization! 我离题了,如果这些是你遇到的最大的性能问题那么你真的没有任何优化问题!

EDIT: 编辑:

I added an extra test here where I used the array passed into the function to call the splice function which showed up as faster than both in both IE, Chrome and Firefox. 在这里添加了一个额外的测试,我使用传递给函数的数组来调用splice函数,它在IE,Chrome和Firefox中都显示出比两者更快的速度。 My conclusion, if you already have the array handy, use it, else use the prototype. 我的结论是,如果你已经有阵列方便,请使用它,否则使用原型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM