Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce vs. Find
(version: 0)
Comparing performance of:
With reduce vs With find
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
let length = 100; const ids = Array.from(Array(length)).map(function (_, i) { return i; }) function random(i) { return Math.ceil(Math.random() * i); } const data = ids.map(function (_, i) { const asset_id = random(100); if (asset_id <= 10) { return {brand_id: 20000 + i, asset_id}; } return {brand_id: i, asset_id}; }) function withReduce() { const brandIdMap = data.reduce((acc, val) => { return { ...acc, [val.brand_id]: val.asset_id, }; }, {}); return ids.map((id) => { if (!brandIdMap[id]) return null; return {id: brandIdMap[id]}; }) } function withFind() { return ids.map((id) => { const value = data.find(({brand_id}) => brand_id === id); if (value) { return {id: value.asset_id}; } return null; }) }
Tests:
With reduce
withReduce();
With find
withFind();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
With reduce
With 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):
**Benchmark Overview** The provided benchmark is designed to compare the performance of two approaches for finding specific data elements within an array: `reduce()` and `find()`. The benchmark creates a dataset of 100 objects, each with a unique brand ID, and then filters out objects based on their brand ID. **Options Compared** Two options are compared: 1. **withReduce()**: Uses the `reduce()` method to create a map of brand IDs to asset IDs. 2. **withFind()**: Uses the `find()` method to search for specific elements within the dataset. **Pros and Cons** * **withReduce()**: + Pros: Can be more efficient when dealing with large datasets, as it avoids unnecessary array searches. It also allows for easy manipulation of the resulting data. + Cons: Can be less intuitive for developers who are not familiar with `reduce()`. Additionally, it can lead to slower performance if the initial data is already sorted or has some inherent structure that can be exploited by `find()`. * **withFind()**: + Pros: Can be more straightforward and easier to understand for developers who are familiar with `find()`. + Cons: May perform slower when dealing with large datasets, as it needs to search the entire array. **Library** The benchmark uses the `Array.prototype.find()` method, which is a part of the ECMAScript standard. This library provides a convenient way to find an element in an array that satisfies a provided condition. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax used in this benchmark. The focus is on comparing two well-known methods for finding data elements within arrays. **Other Considerations** * **Data Distribution**: The generated dataset has a mix of values, which can help to simulate real-world scenarios. * **Number of Iterations**: The number of executions per second (220k and 820k) provides an idea of the performance differences between the two approaches. **Alternatives** Other alternatives for finding data elements within arrays include: * **forEach()**: While not optimized for this use case, `forEach()` can be used to iterate over the array and check each element individually. * **filter()**: Similar to `find()`, but returns an array of all elements that match the condition.
Related benchmarks:
Search: Array to Map and find vs Array.find
Search: Map.get vs Array.find
Map.get versus Array.find for 100 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?