Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs Array.indexOf vs Lodash _.find
(version: 0)
Comparing performance of:
Includes vs _.find vs indexOf
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="lodash.js"></script>
Script Preparation code:
var array = [...Array(100000).keys()];
Tests:
Includes
array.includes(99999);
_.find
_.find(array,n => n === 99999)
indexOf
array.indexOf(99999)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Includes
_.find
indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
974094.1 Ops/sec
_.find
3854.4 Ops/sec
indexOf
311490.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **What is being tested?** The benchmark tests three different approaches to find an element in an array: 1. `array.includes(99999)` 2. `array.indexOf(99999)` 3. `_.find(array, n => n === 99999)` Here's what each approach does: * `array.includes(99999)` checks if the specified value (in this case, 99999) exists in the array. * `array.indexOf(99999)` returns the index of the first occurrence of the specified value (in this case, 99999) in the array. If the value is not found, it returns -1. * `_.find(array, n => n === 99999)` uses the Lodash library to find the first element in the array that matches the provided condition (in this case, `n === 99999`). **Options compared** The benchmark compares the performance of each approach on a large array of 100,000 elements. **Pros and Cons of each approach:** 1. `array.includes(99999)`: * Pros: simple and efficient for small arrays or when you only need to check if an element exists. * Cons: has to iterate over the entire array, making it slower for large arrays. 2. `array.indexOf(99999)`: * Pros: returns the index of the first occurrence of the value, which can be useful in some cases. * Cons: can be slower than `includes` because it has to search through the array to find the element, even if it's not present. 3. `_.find(array, n => n === 99999)`: * Pros: uses a more efficient algorithm that stops iterating as soon as it finds the first matching element. * Cons: requires an additional library (Lodash) and may have a slight performance overhead. **Other considerations** * For small arrays or arrays with a limited number of elements, `array.includes` might be sufficient. * If you need to find the index of an element, `array.indexOf` might be a better choice. * If you're already using Lodash in your project, using `_.find` could be a convenient option. **Library and its purpose** Lodash is a popular JavaScript utility library that provides a wide range of functions for common tasks such as array manipulation, string manipulation, and more. In this benchmark, the `_.find` function is used to find the first element in the array that matches a certain condition. **Special JS feature or syntax** There are no special features or syntax mentioned in the benchmark. However, it's worth noting that some modern browsers (like Chrome) support optional chaining (`?.`) and nullish coalescing (`??`), which can be used to simplify certain types of checks. But this is not relevant to the current benchmark. **Alternatives** Other alternatives for finding an element in an array could include: * Using `Array.prototype.some()` or `Array.prototype.every()` * Creating a custom function using `map()` and `reduce()` * Using a dedicated library like Fast Find or Arrayify Keep in mind that each approach has its own strengths and weaknesses, and the choice of which one to use depends on the specific requirements of your project.
Related benchmarks:
Array.prototype.find vs Lodash find
Array.prototype.some vs Lodash some
Array.prototype.find vs Lodash find 2
find vs lodash find
Comments
Confirm delete:
Do you really want to delete benchmark?