Array.prototype vs [] perf [英] Array.prototype vs [] perf

查看:79
本文介绍了Array.prototype vs [] perf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有机会深入研究这个问题。在调用/应用上下文中使用时性能更高: Array.prototype vs []

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 []?

例如:

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]);

我的想法:我会假设 Array.prototype 方式更高效,因为原型函数可以重用,不需要创建文字。不太确定。

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. Not really sure though.

使用JSPerf(带有chrome)看起来像 Array.prototype 确实稍微多一些表演:

Using JSPerf (with chrome) it looks like the Array.prototype is indeed slightly more performant:

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

推荐答案

这取决于运行它的浏览器。在chrome中,似乎.prototype更快,firefox显示两者之间没有差异,虽然通常比chrome更慢。 IE9显示.prototype的速度有了很大的提升,但到目前为止是最慢的浏览器。

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. IE9 shows a big speed increase for .prototype but is the slowest browser by far.

然而,这种优化是如此之小,以至于人们可能认为节省的时间是偏移的与读取代码所需的额外字节数相对应。我离题了,如果这些是你遇到的最大的性能问题那么你真的没有任何优化问题!

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!

编辑:

我在这里添加了额外的测试我使用传入函数的数组来调用splice函数,它在IE,Chrome和Firefox中都显示出比两者更快的速度。
我的结论是,如果你已经使用了数组,请使用它,否则使用原型。

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. My conclusion, if you already have the array handy, use it, else use the prototype.

这篇关于Array.prototype vs [] perf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆