Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array Objects to Set and has vs Array.find
(version: 0)
Which is fastest? Array of objects, with the ID mapped to `Set` finally looked up with `has`, or directly using `Array.find`
Comparing performance of:
Array to Set and has vs Array.find
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
data = [...Array(100)].map((_, id) => ({ id, data: Math.random() }))
Tests:
Array to Set and has
const response = new Set(data.map(({ id }) => id)).has(95)
Array.find
const response = data.find(({ id }) => id === 95)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array to Set and has
Array.find
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array to Set and has
449493.2 Ops/sec
Array.find
6314651.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark JSON and explain what is being tested, compared, and their pros and cons. **Benchmark Definition:** The benchmark is testing which approach is faster: using `Array.find` to directly look up an ID in an array of objects or creating a Set from an array of IDs and then looking up the ID using `has`. **Script Preparation Code:** The script creates an array of 100 objects, where each object has an `id` property. The `data` variable is created by mapping over the array with an anonymous function that returns an object with `id` and `data` properties. **Html Preparation Code:** There is no HTML preparation code provided, which means that this benchmark is running in a headless browser environment or using a pure JavaScript execution context. **Individual Test Cases:** 1. **Array to Set and has:** This test case creates a Set from an array of IDs (using `map` and `Set`) and then looks up the ID `95` using the `has` method. 2. **Array.find:** This test case uses the `find` method to directly look up the ID `95` in the original array of objects. **Pros and Cons:** * **Array to Set and has:** + Pros: - Can be faster for large datasets since lookup is done using a data structure (Set) that allows for efficient searching. - Can avoid unnecessary iterations over the entire array. - Cons: - Requires creating an additional data structure (Set), which can add overhead in terms of memory allocation and garbage collection. - May not be suitable for smaller datasets or when simplicity is more important than performance. * **Array.find:** + Pros: - Simple and straightforward to implement. - Does not require creating an additional data structure (Set). + Cons: - Can be slower for large datasets since it requires iterating over the entire array until a match is found. - May not be suitable for very large datasets or performance-critical applications. **Library:** The `Array.prototype.find` method uses the ECMAScript 2019 standard (ES2020) syntax and relies on modern JavaScript engines that support this feature. It does not use any external libraries. **Special JS Feature/Syntax:** No special JavaScript features or syntax are used in this benchmark beyond what's required by the `find` method. **Other Alternatives:** * For smaller datasets, using the `indexOf` method (e.g., `array.indexOf(id)` ) could be a viable alternative to both approaches. * In some cases, using a library like Lodash or Ramda might provide additional functionality or optimizations for array-based lookups. Keep in mind that these alternatives may not offer significant performance improvements and should be evaluated on a case-by-case basis.
Related benchmarks:
Search: Array to Map and find vs Array.find
Map.get versus Array.find for 10000 elements
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?