Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map-filter vs reduce (v2)
(version: 0)
Comparing performance of:
indexMap-filter-map vs reducer
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
policiesList=[]; for(i=0; i<1000; i++) policiesList.push({createdBy: String(Number(i)/1000)}); var indexMap = {} var createIndexMap = () => policiesList.forEach((policy, index) => { if (!indexMap[policy.createdBy]) { indexMap[policy.createdBy] = index; } }); var filtering = (policy, index) => index === indexMap[policy.createdBy]; var mapping = policy => policy.createdBy; var reducing= (acc, val) => acc.add(val.createdBy)
Tests:
indexMap-filter-map
createIndexMap(); policiesList.filter(filtering).map(mapping);
reducer
policiesList.reduce(reducing, new Set());
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
indexMap-filter-map
reducer
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):
Let's break down the provided JSON and benchmark. **Benchmark Definition** The test case is designed to compare three approaches for mapping, filtering, and reducing an array of objects: 1. `createIndexMap()`: Creates an index map using the `forEach` method, where each object in the `policiesList` is mapped to its created-by string as a key. 2. `filtering`: Filters the `policiesList` based on the filtering function, which checks if the `createdBy` string exists in the `indexMap`. 3. `mapping`: Maps each object in the `policiesList` to its `createdBy` string using the mapping function. 4. `reducing`: Reduces the `policiesList` using the reducing function, which adds each `createdBy` string to a set. **Comparison Options** The benchmark compares the performance of these four approaches: * **createIndexMap() + filtering + mapping**: Creates an index map and filters based on it. * **reducing**: Uses the reducing function to reduce the list. **Pros and Cons** 1. `createIndexMap() + filtering + mapping`: * Pros: This approach avoids creating a separate set for reducing, which might be slower due to hash table lookups. * Cons: Creating an index map and performing filter operations can lead to additional overhead. 2. **reducing**: * Pros: Utilizes built-in `Set` data structure for fast lookup and insertion operations. * Cons: May require extra memory for the set, and there might be slower hash table lookups. **Library** The benchmark does not explicitly use any external libraries, but it relies on JavaScript's built-in `Set` data structure for the reducing approach. The filtering approach uses a simple function to check if an element exists in the index map, which is likely implemented by the browser or interpreter. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark. It primarily relies on standard JavaScript data structures and functions (e.g., `Array.prototype.forEach`, `Set`, `String` concatenation). **Other Alternatives** If you wanted to explore alternative approaches, here are a few options: 1. **Using a different indexing method**: Instead of creating an index map using `forEach`, you could use `Array.prototype.reduce()` or a library like Lodash's `_.mapObject()`. 2. **Caching the filter results**: If filtering is a performance bottleneck, you could cache the filter results using memoization techniques (e.g., using a `Map` to store the cached results). 3. **Using parallel processing**: If available on your platform, you could use parallel processing libraries like Web Workers or concurrency frameworks to speed up the benchmark. 4. **Optimizing the reducing function**: Depending on the specific requirements and data distribution, you might be able to optimize the reducing function using techniques like lazy evaluation or more efficient set operations. Keep in mind that the optimal approach will depend on the specific use case, data characteristics, and performance requirements.
Related benchmarks:
filter + map vs reduce 123
Filter and Map vs Reduce
flatMap vs reduce vs loop filtering vs filter/map performance
Flat map + filter vs. Reduce
Reduce Push vs. flatMap vs 123
Comments
Confirm delete:
Do you really want to delete benchmark?