You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
lib: prefer using Array#includes instead of Array#indexOf #26568
For semantics, I think it's more appropriate to move them into Array#includes and it seems no performance difference between them at present: https://jsperf.com/array-indexof-vs-includes.
Array#indexOf() will always be faster as it has less to do by spec and the engine is not able to optimize that part away. The difference is not big but it is definitely there.
Array#indexOf() will always be faster as it has less to do by spec and the engine is not able to optimize that part away.
just for the record, includes is actually much simpler in the spec. indexOf does a HasProperty before each Get, while includes just goes straight to Get.
@devsnek you are right, I remembered the spec wrong in this case (I remembered it the other way around). However, in most cases the HasProperty check will just be optimized away by the engine.
For such a tiny op, depend on the use case, IMHO we should always prefer readability, simplicity, and explicivity. If the usage is to simply test for inclusion, and there is no use of the found index, then .include is optimal.
One main reason for this is that our stdlib is used as a reference implementation.
Activity
mscdex commentedon Mar 10, 2019
Assuming you tested the equivalent of master, did you also check the versioned branches? It may/may not be as fast in older versions of V8.
starkwang commentedon Mar 10, 2019
I've tested it on LTS version (code). Here is the result:
Node.js 6.17.0 (
indexOfis 3x faster):Node.js 8.15.1 (same):
Node.js 10.15.3 (
includesis 80x faster):Node.js 11.11.0 (
includesis 80x faster):So we may not land this change into 6.x.
BridgeAR commentedon Mar 10, 2019
@starkwang the actual code is optimized away in your benchmark. Therefore it is significantly faster.
BridgeAR commentedon Mar 10, 2019
Array#indexOf()will always be faster as it has less to do by spec and the engine is not able to optimize that part away. The difference is not big but it is definitely there.I do not think we should change this ever.
BridgeAR commentedon Mar 10, 2019
Yields on master:
devsnek commentedon Mar 10, 2019
just for the record, includes is actually much simpler in the spec. indexOf does a HasProperty before each Get, while includes just goes straight to Get.
on v11.11.0 i get:

the difference is negligible
BridgeAR commentedon Mar 10, 2019
@devsnek you are right, I remembered the spec wrong in this case (I remembered it the other way around). However, in most cases the
HasPropertycheck will just be optimized away by the engine.V8 7.3 seems to be on par performance wise.
refack commentedon Mar 10, 2019
For such a tiny op, depend on the use case, IMHO we should always prefer readability, simplicity, and explicivity. If the usage is to simply test for inclusion, and there is no use of the found index, then
.includeis optimal.One main reason for this is that our stdlib is used as a reference implementation.
https://jsperf.com/array-indexof-vs-includes/25 that indicate +/- 10% on my chrome's V8 7.2
lib: use Array#includes instead of Array#indexOf