Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

javascript - Array indexOf() vs includes() perfomance depending on browser and needle position

Is there any advantage to Array.prototype.includes() over Array.prototype.indexOf() depending on browsers (Chrome, Firefox) and needle item position (at the begging, middle, ending of the array)?

Array.prototype.includes vs. Array.prototype.indexOf There is no browser specific information, there is no position in the array specific information, and I don't ask about NaN value.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I made a test using array with 10 000 numeric values, here is results:

Chrome:

  • beginning
    • includes (22,043,904 ops/sec)
    • indexOf (136,512,737 ops/sec)
  • middle
    • includes (8,361 ops/sec)
    • indexOf (31,296 ops/sec)
  • ending
    • includes (4,018 ops/sec)
    • indexOf (95,221 ops/sec)

Firefox:

  • beginning
    • includes (34,087,623 ops/sec)
    • indexOf (33,196,839 ops/sec)
  • middle
    • includes (84,880 ops/sec)
    • indexOf (86,612 ops/sec)
  • ending
    • includes (25,253 ops/sec)
    • indexOf (14,994 ops/sec)

So, indexOf() in Chrome works much faster than includes() in all positions.

In Firefox both indexOf() and includes() works almost similar.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...