Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce Map vs Reduce Object 4
(version: 3)
Comparing performance of:
Reduce Map vs Reduce Object
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var variants = [ { gateDetails: [ { channelIds: [ "srcl", "tkpd" ], gateId: "khusus-pi" }, { channelIds: [ "srcl", "tkpd" ], gateId: "khusus-pi-2" } ] }, { gateDetails: [ { channelIds: [ "srcl", "tkpd" ], gateId: "khusus-pi" }, { channelIds: [ "srcl", "tkpd" ], gateId: "khusus-pi-2" } ] } ];
Tests:
Reduce Map
const result = variants.reduce((gates, nextVariant) => { nextVariant.gateDetails.forEach(gateDetail => { if (gates.every(gate => gate.gateId !== gateDetail.gateId)) { gates.push({ channelIDs: gateDetail.channelIds, gateId: gateDetail.gateId, }); } else { gates = gates.map(gate => { if ( gate.gateId === gateDetail.gateId && gateDetail.channelIds && gate.channelIDs ) { const newChannelIds = gateDetail.channelIds.filter( channelId => !gate.channelIDs.includes(channelId) ); return { ...gate, channelIDs: [ ...gate.channelIDs, ...newChannelIds, ], }; } return gate; }); } }); return gates; },[]);
Reduce Object
const objGate = variants.reduce((gate, variant) => { for(const gateDetail of variant.gateDetails) { if(gate[gateDetail.gateId]) { const newChannelIds = gateDetail.channelIds.filter( channelId => !gate[gateDetail.gateId].channelIDs.includes(channelId) ); if(!newChannelIds.length) { continue } gate[gateDetail.gateId] = { ...gate[gateDetail.gateId], channelIDs: [ ...gate[gateDetail.gateId].channelIDs, ...newChannelIds ] } }else { gate[gateDetail.gateId] = { gateId: gateDetail.gateId, channelIDs: gateDetail.channelIds } } } return gate }, {}) const gates = Object.keys(objGate).map(gateId => objGate[gateId] );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Reduce Map
Reduce Object
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 dive into the benchmark definition and test cases. **Benchmark Definition** The benchmark measures the performance of two approaches to process an array of gate details: 1. `Reduce Map`: * Uses the `reduce()` method on the `variants` array, which iterates over each element (a variant) and applies a callback function to each `gateDetail`. * The callback function checks if there's a duplicate `gateId` among all previous elements in the accumulator (`gates`). If not, it adds a new entry with the current `gateDetail` as a key. * If there are duplicates, it merges the channels by filtering out the ones that don't match the current `gateDetail`. 2. `Reduce Object`: * Uses the `reduce()` method on the `variants` array, similar to the previous approach. * However, this time it iterates over each `variant` and applies a callback function to each `gateDetail`. This function checks if there's already an entry in the accumulator (`objGate`) with the current `gateId`. * If not, it creates a new entry with the current `gateId` and merges the channels. * If there are duplicates, it updates the existing entry by merging the channels. **Options Compared** The two approaches compare different ways to process the array of gate details. Here are some pros and cons of each approach: 1. **Reduce Map** * Pros: + More concise and readable code. + Easier to understand and maintain, as it uses a standard `reduce()` method. * Cons: + Might be slower due to the overhead of checking for duplicate `gateId`s. 2. **Reduce Object** * Pros: + Can potentially be faster, as it avoids the need to check for duplicates using `every()`. * Cons: + The code is more complex and harder to read, especially when dealing with nested objects. **Libraries Used** In this benchmark, no specific libraries are used. However, some JavaScript features are used that might not be immediately apparent: 1. **Template Literals**: Used in the `Script Preparation Code` section to define the `variants` array. 2. **Spread Operator (`...`)**: Used to merge objects and arrays. **Special JS Features/Syntax** None of the test cases use any special JavaScript features or syntax that's not already explained above. **Alternatives** Some alternative approaches could be explored, such as: 1. **Using `forEach()` instead of `reduce()`:** * Could simplify the code but might lead to slower performance due to the overhead of function calls. 2. **Using `filter()` and `some()` instead of `every()`:** * Could make the code more concise but might lead to slower performance due to the overhead of filtering and checking for duplicates. 3. **Using a library like Lodash or Ramda:** * Could provide additional functionality and optimization opportunities, but might also introduce unnecessary dependencies. Keep in mind that these alternatives would require significant changes to the benchmark definition and test cases, and their impact on performance would depend on various factors, including the specific use case and implementation details.
Related benchmarks:
reduce (immutable) vs map + fromEntries
Object.fromEntries vs reduce v21
lodash entries vs object entries
Object.fromEntries vs reduce round 2
Flatmap vs reduce with objects
Comments
Confirm delete:
Do you really want to delete benchmark?