Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Includes vs Find (Javascript)
(version: 0)
Array.includes vs Array.find
Comparing performance of:
Array.Includes vs Array.find
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var hasZero = []; var withoutZero = []; for (let i = 0; i < 10000; i++) { hasZero.push(Math.floor(Math.random() * 1000)); withoutZero.push(Math.floor(Math.random() * 1000) + 1) }
Tests:
Array.Includes
const tempResult = !!Math.round(Math.random()) ? hasZero.some(v => v === 0) : withoutZero.includes(v => v === 0);
Array.find
const tempResult = !!Math.round(Math.random()) ? hasZero.some(v => v === 0) : withoutZero.find(v => v === 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.Includes
Array.find
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that compares two approaches for finding an element in an array: `Array.includes()` and `Array.find()`. The benchmark is designed to test the performance of these methods on large arrays with random elements. **Options Compared** Two options are compared: 1. **`Array.includes()`**: This method checks if a specified value (in this case, 0) exists in the array. 2. **`Array.find()`**: This method returns the first element in the array that satisfies a provided condition (in this case, `v === 0`). If no such element is found, it returns undefined. **Pros and Cons** * **`Array.includes()`**: * Pros: It's generally faster and more concise than `Array.find()`. * Cons: `includes()` only checks for the existence of a single value in the array, whereas `find()` can be used to find any element that satisfies a condition. * **`Array.find()`**: * Pros: Can be used to find multiple elements or complex conditions. * Cons: Typically slower than `includes()`, especially for large arrays. **Library and Purpose** In this benchmark, the `some()` function is used in conjunction with `includes()`. The `some()` function returns true if at least one element in the array satisfies the provided condition. However, since `includes()` already checks for existence, using `some()` inside it does not provide a significant performance gain. **Special JS Features** There are no special JavaScript features or syntax used in this benchmark, making it accessible to a wide range of software engineers. **Other Alternatives** For larger arrays, other approaches can be considered: 1. **`Array.prototype.every()`**: This method returns true if all elements in the array satisfy the provided condition. 2. **`Array.prototype.filter()`**: This method creates a new array with all elements that satisfy the provided condition. 3. **Custom solution**: For very large arrays, using a custom solution like binary search or hash tables might be more efficient. Note that these alternatives are typically used for specific use cases and may not always provide better performance than `Array.includes()` and `Array.find()`.
Related benchmarks:
Some vs. Filter vs. indexOf vs. Includes2 vs. Find
MapIncludes vs Find on collection
Some vs. Filter vs. indexOf vs. Map+Includes vs. Find
Array Includes vs. Find
Comments
Confirm delete:
Do you really want to delete benchmark?