Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes vs find
(version: 0)
Banana2
Comparing performance of:
indexOf vs Includes vs find
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = ['banana', 'sausage', 'jesus']
Tests:
indexOf
array.indexOf('sausage') !== 1
Includes
array.includes('sausage')
find
array.find(item => item === 'sausage')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
indexOf
Includes
find
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
53176404.0 Ops/sec
Includes
49341968.0 Ops/sec
find
21930302.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and analyze what's being tested. **What is being tested?** The provided JSON represents a microbenchmark that compares three different methods for searching an element within an array: `indexOf`, `includes`, and `find`. Each test case measures the execution time of these methods on a specific input string ("sausage") in a given JavaScript array. **Options compared** * `indexOf`: A method that returns the index of the first occurrence of the specified value. If the value is not found, it returns `-1`. * `includes`: A method that checks if the array includes the specified value without returning its index. * `find`: A method that finds the first element in the array that satisfies the provided condition. **Pros and Cons** * **`indexOf`**: Pros: Simple and efficient for finding the exact index of an element. Cons: Returns `-1` if not found, which can be misleading. Also, it only searches for a single value. * **`includes`**: Pros: Faster than `indexOf` for larger arrays or when searching for multiple values. Cons: Returns `true` or `false`, but does not return the index. * **`find`**: Pros: More flexible and expressive than `indexOf`, as it returns the actual element if found. Cons: Slower than both `indexOf` and `includes` due to its iterative search. **Library and purpose** There is no library used in this benchmark. The methods being compared are built-in JavaScript methods, which means they are implemented natively by the JavaScript engine. **Special JS features or syntax** None mentioned. **Other alternatives** If you were to implement a custom solution for these search methods, some alternative approaches might include: * Using `Array.prototype.some()` or `Array.prototype.every()` to achieve similar results as `includes`. * Writing a simple loop to find the first matching element, similar to `find`. * Utilizing more advanced techniques like binary searching (which is not applicable in this case due to the sequential nature of these methods). For example, an alternative implementation for `indexOf` could involve using a binary search algorithm. However, given the simplicity and directness of the built-in methods, it's unlikely that a custom solution would offer significant performance improvements. In summary, MeasureThat.net is testing the relative performance of three basic JavaScript array methods: `indexOf`, `includes`, and `find`. The benchmark provides insight into which method is fastest for searching an element in an array under specific conditions.
Related benchmarks:
IndexOf vs Includes
find vs includes
IndexOf vs Includes str
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?