数组indexOf()vs include()性能取决于浏览器和指针位置 [英] Array indexOf() vs includes() perfomance depending on browser and needle position

查看:75
本文介绍了数组indexOf()vs include()性能取决于浏览器和指针位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与Array.prototype.indexOf()相比,Array.prototype.includes()相对于浏览器(Chrome,Firefox)和针头位置(在数组的开始,中间,结尾处)有什么优势吗?

Array.prototype.includes与Array.prototype.indexOf 没有浏览器特定的信息,数组特定信息中没有位置,并且我不询问 NaN 值.

解决方案

我使用具有10000个数字值的数组进行了测试,结果如下:

Chrome浏览器:

  • 开始
    • 包括(22,043,904 ops/sec)
    • indexOf(136,512,737 ops/sec)
    • 包括(每秒8,361次操作)
    • indexOf(31,296 ops/sec)
  • 结束
    • 包括(4,018次操作/秒)
    • indexOf(95,221 ops/sec)

Firefox:

  • 开始
    • 包括(34,087,623个操作/秒)
    • indexOf(33,196,839 ops/sec)
    • 包括(每秒84,880次操作)
    • indexOf(86,612 ops/sec)
  • 结束
    • 包括(25,253次操作/秒)
    • indexOf(14,994 ops/sec)

因此,Chrome中的 indexOf()在所有位置上的运行速度都快于 includes().

在Firefox中, indexOf() includes()的工作原理几乎相似.

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.

解决方案

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.

这篇关于数组indexOf()vs include()性能取决于浏览器和指针位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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