Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array.prototype.includes vs. Array.prototype.indexOf

People also ask

Which is better indexOf or includes?

I would suggest using the includes() method to check if the element is present in the array. If you need to know where the element is in the array, you need to use the indexOf() method.

Which is faster indexOf or includes?

History. indexOf was created way before includes . Seems like the difference in performance is not that obvious. My mobile Firefox shows that indexOf is actually faster.

What is the difference between findIndex and indexOf?

findIndex - Returns the index of the first element in the array where predicate is true, and -1 otherwise. indexOf - Returns the index of the first occurrence of a value in an array.

Can you use indexOf for an array?

Introduction to the JavaScript array indexOf() methodTo find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found.


tl;dr: NaN is treated differently:

  • [NaN].indexOf(NaN) > -1 is false
  • [NaN].includes(NaN) is true

From the proposal:

Motivation

When using ECMAScript arrays, it is commonly desired to determine if the array includes an element. The prevailing pattern for this is

if (arr.indexOf(el) !== -1) {
    ...
}

with various other possibilities, e.g. arr.indexOf(el) >= 0, or even ~arr.indexOf(el).

These patterns exhibit two problems:

  • They fail to "say what you mean": instead of asking about whether the array includes an element, you ask what the index of the first occurrence of that element in the array is, and then compare it or bit-twiddle it, to determine the answer to your actual question.
  • They fail for NaN, as indexOf uses Strict Equality Comparison and thus [NaN].indexOf(NaN) === -1.

Proposed Solution

We propose the addition of an Array.prototype.includes method, such that the above patterns can be rewritten as

if (arr.includes(el)) {
    ...
}

This has almost the same semantics as the above, except that it uses the SameValueZero comparison algorithm instead of Strict Equality Comparison, thus making [NaN].includes(NaN) true.

Thus, this proposal solves both problems seen in existing code.

We additionally add a fromIndex parameter, similar to Array.prototype.indexOf and String.prototype.includes, for consistency.


Further information:

  • SameValueZero algorithm
  • Strict Equality Comparison algorithm

Technically, the only difference is how NaN and undefined are treated. They will not be findable when using indexOf

arr.indexOf('blah') !== -1 is less more readable and maintainable. On the other hand, arr.includes('blah') does what it says and it is obvious that it returns a boolean.

includes also is of no use if you want to know where the string is in the array.

Concerning performances: according to this article on the subject there are no noticeable difference although includes may be a very little bit slower.

indexOf was created way before includes.


.indexOf() and .includes() methods can be used to search for an element in an array or to search for a character/substring in a given string.

Usage in Array

(Link to ECMAScript Specification)

  1. indexOf uses Strict Equality Comparison whereas includes uses the SameValueZero algorithm. Because of this reason, the following two points of differences arise.

  2. As pointed out by Felix Kling, the behavior is different in case of NaN.

let arr = [NaN];

arr.indexOf(NaN); // returns -1; meaning NaN is not present
arr.includes(NaN); // returns true
  1. The behavior is also different in case of undefined.
let arr = [ , , ];

arr.indexOf(undefined); // returns -1; meaning undefined is not present
arr.includes(undefined); // returns true

Usage in String

(Link to ECMAScript Specification)

1. If you pass a RegExp to indexOf, it will treat the RegExp as a string and will return the index of the string, if found. However, if you pass a RegExp to includes, it will throw an exception.

let str = "javascript";

str.indexOf(/\w/); // returns -1 even though the elements match the regex because /\w/ is treated as string
str.includes(/\w/); // throws TypeError: First argument to String.prototype.includes must not be a regular expression

Performance

As GLAND_PROPRE pointed out, includes may be a little (very tiny) bit slower (for it needs to check for a regex as the first argument) than indexOf but in reality, this doesn't make much difference and is negligible.

History

String.prototype.includes() was introduced in ECMAScript 2015 whereas Array.prototype.includes() was introduced in ECMAScript 2016. With regards to browser support, use them wisely.

String.prototype.indexOf() and Array.prototype.indexOf() are present in ES5 edition of ECMAScript and hence supported by all browsers.


Conceptually you should use indexOf when you want to use the position indexOf just give you to extract the value or operate over the array, i.e using slice, shift or split after you got the position of the element. On the other hand, Use Array.includes only to know if the value is inside the array and not the position because you don't care about it.