Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Array IndexOf vs includes vs findIndex vs find 3
(version: 0)
Comparing performance of:
indexOf vs includes vs find vs findIndex
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; while (i < 15001) {arr[i] =i.toString(); i++};
Tests:
indexOf
const index = arr.indexOf("15000");
includes
const included = arr.includes("15000");
find
const item = arr.find(item => item === "15000");
findIndex
const index = arr.findIndex(item => item === "15000");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
indexOf
includes
find
findIndex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
28 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36 Edg/146.0.0.0
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
61311.3 Ops/sec
includes
63050.8 Ops/sec
find
21053.1 Ops/sec
findIndex
22511.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Benchmark Overview** The benchmark measures the performance of four different methods to search for an element in an array: 1. `indexOf()` 2. `includes()` 3. `find()` 4. `findIndex()`` These methods are commonly used in JavaScript to find a specific value within an array. **Method Descriptions and Comparison** Here's a brief overview of each method: * **`indexOf()`**: This method returns the index of the first occurrence of a specified element in an array. If the element is not found, it returns -1. * **`includes()`**: This method checks if an element exists within an array. It returns true if the element is found, and false otherwise. * **`find()`**: This method finds the value at the index of the specified element in an array. If no such index exists, it returns undefined. * **`findIndex()`**: Similar to `indexOf()`, but returns the index of the first occurrence of a specified element. **Options Compared** The benchmark compares the performance of these four methods when searching for a specific value (`"15000"` in this case) within an array of 15,001 elements. The options are compared in terms of: * **Number of executions per second**: This measures how many times each method executes per second on the same dataset. * **Execution time**: Although not explicitly measured, we can infer that `includes()` is likely to be faster than `indexOf()` and `findIndex()`, which require more computations. **Pros and Cons** Here's a brief analysis of the pros and cons for each method: * **`indexOf()`**: Pros: Generally fast. Cons: Returns -1 if not found, which can be misleading. * **`includes()`**: Pros: Faster than `indexOf()` and `findIndex()`. Cons: May trigger more computations due to its nature of iterating through the array. * **`find()`** and **`findIndex()`**: These methods are similar but have different return types. Generally faster than `indexOf()`, but might be slower for very large arrays. Keep in mind that these optimizations can be highly dependent on specific use cases, data distributions, and browser implementations. **Library Used (None)** No specific libraries are mentioned or used in this benchmark. **Special JS Feature/Syntax** The use of arrow functions (`item => item === "15000"`) is a modern JavaScript syntax introduced in ECMAScript 2015. It's a concise way to define small, one-time-use functions. **Alternatives** Some alternatives to the above methods include: * **`binarySearch()`**: This method uses a binary search algorithm to find an element within an array, which can be faster for large arrays but has different usage patterns. * **`filter()`**: While not designed for direct value lookup like `indexOf()`, `includes()`, and `findIndex()`, it can be used creatively for similar purposes. * **Other specialized algorithms or library functions**, such as those optimized for specific array types (e.g., sparse arrays) or data structures, might provide better performance depending on the specific requirements of your application. The alternatives are not directly comparable to `indexOf()` and `includes()`, but they may offer different trade-offs in terms of readability, flexibility, or performance. In summary, this benchmark provides a useful comparison of four commonly used JavaScript methods for searching elements within arrays. The results help identify which method is likely to be the fastest for specific use cases, keeping in mind that actual performance can vary based on many factors.
Related benchmarks:
findIndex vs indexOf for simple array 2
Array find with indexOf vs includes
JS Array IndexOf vs includes vs findIndex vs find 2
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?