Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce Object vs Reduce Map
(version: 0)
Comparing performance of:
Reduce + Every + Map vs Reduce + Object
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var channelStocks = [ { gateId: "khusus-pi", channel: "srcl", stock: 1 }, { gateId: "khusus-pi", channel: "tkpd", stock: 2 }, { gateId: "khusus-pi", channel: "shpe", stock: 3 }, { gateId: "khusus-pi", channel: "lzda", stock: 4 }, { gateId: "khusus-pi-2", channel: "srcl", stock: 10 }, { gateId: "khusus-pi-2", channel: "tkpd", stock: 20 }, { gateId: "khusus-pi-2", channel: "shpe", stock: 30 }, { gateId: "khusus-pi-2", channel: "lzda", stock: 40 }, { gateId: "khusus-pi-3", channel: "srcl", stock: 100 }, { gateId: "khusus-pi-3", channel: "tkpd", stock: 200 }, { gateId: "khusus-pi-3", channel: "shpe", stock: 300 }, { gateId: "khusus-pi-3", channel: "lzda", stock: 400 }, ];
Tests:
Reduce + Every + Map
var gateStocks = channelStocks.reduce((result, nextChannelStock) => { if ( result.every( gateStock => gateStock.gateId !== nextChannelStock.gateId ) ) { result.push({ gateId: nextChannelStock.gateId, details: [ { stocks: nextChannelStock.stock, channelId: nextChannelStock.channel, }, ], }); return result; } return result.map(gateStock => { if ( gateStock.gateId === nextChannelStock.gateId ) { return { ...gateStock, details: [ ...gateStock.details, { stocks: nextChannelStock.stock, channelId: nextChannelStock.channel, }, ], }; } return gateStock; }); },[]);
Reduce + Object
var gateStocks = channelStocks.reduce((gateStock, channelStock) => { if(gateStock[channelStock.gateId]) { gateStock[channelStock.gateId] = { ...gateStock[channelStock.gateId], details: [ ...gateStock[channelStock.gateId].details, { stock: channelStock.stock, channelId: channelStock.channel, } ] } return gateStock; } gateStock[channelStock.gateId] = { gateId: channelStock.gateId, details: [{ stock: channelStock.stock, channelId: channelStock.channel, }] } return gateStock; }, {}) const result = Object.keys(gateStocks).map(gateId => gateStocks[gateId]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Reduce + Every + 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):
Measuring the performance of JavaScript code is crucial for optimizing applications, and MeasureThat.net provides a valuable platform for benchmarking. The provided JSON represents two test cases: "Reduce Object" and "Reduce Every + Map". These benchmarks compare different approaches to reduce an array of objects. **Test Case 1: Reduce Object** This test case uses the `Array.prototype.reduce()` method with a custom callback function. The callback function checks if every gate stock in the result has a unique gate ID, and if so, pushes a new object with the gate ID and stock details. If not, it updates the existing stock details. **Test Case 2: Reduce Every + Map** This test case also uses the `Array.prototype.reduce()` method but with two additional steps: 1. It first reduces the array of channel stocks using the same custom callback function as in Test Case 1. 2. Then, it maps over the resulting object and extracts the key-value pairs. **Options compared:** * **Reduce Object**: This approach involves creating a new object and pushing or updating its properties based on the condition. * **Reduce Every + Map**: This approach first reduces the array to an object and then maps over that object to extract specific key-value pairs. **Pros and Cons:** * **Reduce Object**: + Pros: - Creates a compact object with only unique gate stocks. - Less memory-intensive, especially for large datasets. + Cons: - May require more CPU cycles due to the push or update operations. - Can lead to slower performance if the resulting object is too large. * **Reduce Every + Map**: + Pros: - Allows for easier filtering and extraction of specific data. - Can be faster if the resulting object is smaller. + Cons: - Creates a new array with extracted key-value pairs, which can be memory-intensive. - May lead to slower performance due to the additional mapping step. **Other considerations:** * Both approaches have their trade-offs in terms of performance and memory usage. The choice between them depends on the specific requirements and constraints of the application. * In general, creating a compact object with unique gate stocks can be beneficial for data processing and filtering. * Using `Reduce Every + Map` can provide more flexibility when working with large datasets or requiring specific data extraction. **Alternatives:** * Other approaches to reduce an array of objects include: + Using the `Array.prototype.every()` method in combination with the spread operator (`...`) to create a new object. + Utilizing libraries like Lodash, which provides optimized functions for working with arrays and objects. + Exploring alternative data structures, such as sets or maps, depending on the specific use case. In conclusion, the choice between "Reduce Object" and "Reduce Every + Map" depends on the application's requirements and constraints. By understanding the pros and cons of each approach, developers can optimize their code for better performance and memory usage.
Related benchmarks:
Object.fromEntries vs reduce v3
map vs reduce at mapping
Object.fromEntries vs reduce v21
lodash entries vs object entries
fromEntries vs reduce fight!
Comments
Confirm delete:
Do you really want to delete benchmark?