Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
find vs includes vs indexof
(version: 0)
Comparing performance of:
find vs includes vs indexOf
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = ['banana', 'sausage', 'jesus']
Tests:
find
array.find(f => f == 'banana' );
includes
array.includes('banana');
indexOf
array.indexOf('banana');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
find
includes
indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
find
69417216.0 Ops/sec
includes
61756584.0 Ops/sec
indexOf
71502640.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance of three different methods to search for a specific value in an array: 1. `array.find()` 2. `array.includes()` 3. `array.indexOf()` These methods are used to find the index of the first occurrence of a specified element within an array, similar to the traditional `indexOf()` method. **Method Comparison** The three methods being compared differ in their approach and behavior: 1. **`find()`**: This method returns the first element that satisfies the provided condition (in this case, `f == 'banana'`). If no such element exists, it returns `undefined`. Unlike `includes()` and `indexOf()`, `find()` does not search the entire array; it only searches until it finds a match. 2. **`includes()`**: This method checks if an element is present in the array using a simple membership test. It iterates through the array to find the first occurrence of the specified value, returning `true` if found or `false` otherwise. 3. **`indexOf()`**: Similar to `find()`, this method returns the index of the first occurrence of the specified element within the array. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **`find()`**: * Pros: more concise, easier to use with arrow functions. * Cons: may return `undefined` if no match is found, can be slower for large arrays due to early termination. 2. **`includes()`**: * Pros: simple, efficient, and widely supported. * Cons: may be slower than `indexOf()` for very large arrays, as it iterates through the array even after finding a match. 3. **`indexOf()`**: * Pros: reliable, fast, and well-established. * Cons: can be less concise and more verbose compared to `find()`, requires explicit index management. **Library Usage** None of the methods rely on external libraries, making them self-contained and easy to use with minimal setup. **Special JS Features/Syntax** None of the methods rely on special JavaScript features or syntax that would require additional explanation. They are all standard method calls and can be used without any additional context. **Alternatives** Other approaches to searching for an element in an array include: 1. **`for...of` loop**: A more traditional, explicit way to iterate through the array. 2. **`Array.prototype.forEach()`**: A callback-based approach that allows iterating through the array while executing a custom function for each element. 3. **Native `binarySearch()` method** (not shown in this benchmark): An optimized algorithm for finding an element in a sorted array. The specific choice of method ultimately depends on the desired behavior, performance characteristics, and personal preference.
Related benchmarks:
IndexOf vs Includes
find vs includes
IndexOf vs Includes vs find
my own array: indexOf vs includes vs some
Comments
Confirm delete:
Do you really want to delete benchmark?