Why is looping through an Array so much faster than JavaScript's native `indexOf`?

后端 未结 7 1845
逝去的感伤
逝去的感伤 2020-11-29 03:15

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 nati

相关标签:
7条回答
  • 2020-11-29 04:02

    Run the test one more time with the edits I've made.

    I've increased the size of the array, and made the index you're searching for larger as well. It seems in large arrays indexOf may be a faster choice.

    http://jsben.ch/#/xm2BV

    EDIT: Based on more tests, indexOf seems to run faster than a for loop in the version of Safari I'm using (5.0.3) and slower in just about everything else.

    0 讨论(0)
提交回复
热议问题