Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Search in array of objects using Find, FindIndex, flatMap with includes
(version: 0)
Comparing performance of:
Find vs FindIndex vs flatMap with includes
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.data = [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Charlie' } ]; window.id = 8;
Tests:
Find
const exists = data.find(item => item.id === id) !== undefined
FindIndex
const exists = data.findIndex(item => item.id === id) !== -1
flatMap with includes
const exists = data.flatMap(item => item.id).includes(id)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Find
FindIndex
flatMap with includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; SAMSUNG SM-J810G) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/16.0 Chrome/92.0.4515.166 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 92 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Find
121655.0 Ops/sec
FindIndex
150798.5 Ops/sec
flatMap with includes
86343.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is tested in the provided JSON?** The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net. The benchmark tests three different approaches for searching an array of objects using various methods: 1. `find()`: A method that returns the value of the first element in the array that satisfies the provided condition. 2. `findIndex()`: A method that returns the index of the first element in the array that satisfies the provided condition. 3. `flatMap()` with `includes()`: A method combination that flattens an array of arrays into a single array and then uses the `includes()` method to check if an element exists. **Options compared** The benchmark compares the performance of these three approaches: * `find()` * `findIndex()` * `flatMap()` with `includes()` **Pros and cons of each approach:** 1. **`find()`**: * Pros: Simple, intuitive, and fast for small arrays. * Cons: Can be slow for large arrays because it uses a linear search algorithm (O(n)). 2. **`findIndex()`**: * Pros: Faster than `find()` for large arrays since it returns the index of the element instead of the entire array. * Cons: Still has a time complexity of O(n), making it slower than `flatMap()` with `includes()` for very large arrays. 3. **`flatMap()` with `includes()`**: * Pros: Can be faster and more efficient than `find()` and `findIndex()` for very large arrays because it uses a linear scan algorithm (O(n)) on the flattened array. * Cons: May require more memory to flatten the array, which can be a concern for large datasets. **Library usage** None of the benchmark test cases use any external libraries. The tests are written in pure JavaScript. **Special JS feature/syntax** There is no special JavaScript feature or syntax used in these test cases beyond what's standard for JavaScript arrays and functions. **Other alternatives** If `flatMap()` with `includes()` isn't suitable for your specific use case, you can also consider: * Using `filter()` instead of `find()` and `findIndex()`. While not as efficient as `flatMap()` with `includes()`, it still offers a good balance between simplicity and performance. * Utilizing modern JavaScript features like `Array.prototype.some()` or `Array.prototype.every()`, which provide more concise alternatives to traditional loop-based approaches. However, if you're dealing with large arrays or need optimal performance, using optimized libraries like Lodash (`findIndex()`) or avoiding unnecessary array flattening can often lead to better results.
Related benchmarks:
Find item in large array - Fork
Search: Array to Map and find vs Array.find
Search: Array to Map and find vs Array.find685000
Search: Array to Map and find vs Array.find 2
Comments
Confirm delete:
Do you really want to delete benchmark?