为什么循环数组比JavaScript的本机`indexOf`快得多? [英] Why is looping through an Array so much faster than JavaScript's native `indexOf`?

查看:111
本文介绍了为什么循环数组比JavaScript的本机`indexOf`快得多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么循环数组的速度比JavaScript的原生 indexOf 快得多?是否有错误或我不考虑的事情?我希望本机实现会更快。

  For Loop While Loop indexOf 
Chrome 10.0 50,948,997 111,272,979 12,807,549
Firefox 3.6 9,308,421 62,184,430 2,089,243
Opera 11.10 11,756,258 49,118,462 2,335,347



Chrome版本49.0.2623.87(64位)


Why is looping through an Array so much faster than JavaScript's native indexOf? Is there an error or something that I'm not accounting for? I expected native implementations would be faster.

                For Loop        While Loop      indexOf
Chrome 10.0     50,948,997      111,272,979     12,807,549
Firefox 3.6     9,308,421       62,184,430      2,089,243
Opera 11.10     11,756,258      49,118,462      2,335,347   

http://jsben.ch/#/xm2BV

解决方案

5 years from then, lot of changes happened in browsers. Now, indexOf performance has increased and is definitely better than any other custom alternative.

Chrome Version 49.0.2623.87 (64-bit)

这篇关于为什么循环数组比JavaScript的本机`indexOf`快得多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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