Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Performance for loop vs map
(version: 0)
Comparing performance of:
For loop. vs Map
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
For loop.
const getProductionBarChartData = (groupedProductions) => { const result = { solarElectricity: [], wasteOutput: [], date: [] } for (const key in groupedProductions) { const entry = groupedProductions[key] result.solarElectricity.push(entry.solarElectricity) result.wasteOutput.push(entry.wasteOutput) result.date.push(new Date(key)) } return result } getProductionBarChartData({ '2020-10': { solarElectricity: 197.9, wasteOutput: 104.2 }, '2020-08': { solarElectricity: 109.39999999999999, wasteOutput: 224.59999999999997 }, '2020-02': { solarElectricity: 124.5, wasteOutput: 43.7 }, '2020-12': { solarElectricity: 278.3, wasteOutput: 131 }, '2020-04': { solarElectricity: 120.3, wasteOutput: 108.8 }, '2020-03': { solarElectricity: 433.49999999999994, wasteOutput: 335.59999999999997 }, '2020-07': { solarElectricity: 130.70000000000002, wasteOutput: 154.70000000000002 }, '2020-01': { solarElectricity: 180.9, wasteOutput: 148.5 }, '2020-09': { solarElectricity: 374.9, wasteOutput: 349.5 }, '2020-11': { solarElectricity: 48.300000000000004, wasteOutput: 69.1 }, '2020-05': { solarElectricity: 98.2, wasteOutput: 30.3 }, '2020-06': { solarElectricity: 152.3, wasteOutput: 123.7 } })
Map
const getProductionBarChartData2 = (groupedProductions) => { return { solarElectricity: Object.keys(groupedProductions).map( (key) => groupedProductions[key].solarElectricity ), wasteOutput: Object.keys(groupedProductions).map( (key) => groupedProductions[key].wasteOutput ), date: Object.keys(groupedProductions).map((key) => new Date(key)) } } getProductionBarChartData2({ '2020-10': { solarElectricity: 197.9, wasteOutput: 104.2 }, '2020-08': { solarElectricity: 109.39999999999999, wasteOutput: 224.59999999999997 }, '2020-02': { solarElectricity: 124.5, wasteOutput: 43.7 }, '2020-12': { solarElectricity: 278.3, wasteOutput: 131 }, '2020-04': { solarElectricity: 120.3, wasteOutput: 108.8 }, '2020-03': { solarElectricity: 433.49999999999994, wasteOutput: 335.59999999999997 }, '2020-07': { solarElectricity: 130.70000000000002, wasteOutput: 154.70000000000002 }, '2020-01': { solarElectricity: 180.9, wasteOutput: 148.5 }, '2020-09': { solarElectricity: 374.9, wasteOutput: 349.5 }, '2020-11': { solarElectricity: 48.300000000000004, wasteOutput: 69.1 }, '2020-05': { solarElectricity: 98.2, wasteOutput: 30.3 }, '2020-06': { solarElectricity: 152.3, wasteOutput: 123.7 } })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
For loop.
Map
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. **What is being tested?** The benchmark measures the performance difference between using a `for` loop and the `map()` function to process an array of objects in JavaScript. **Options compared:** Two options are being compared: 1. **For loop**: A traditional `for` loop that iterates over the properties of an object (`groupedProductions`) and pushes values into arrays (`result.solarElectricity`, `result.wasteOutput`, and `result.date`). 2. **Map**: The `map()` function, which applies a transformation to each element in an array (in this case, extracting values from objects) and returns a new array. **Pros and Cons of each approach:** 1. **For loop**: * Pros: + Can be more intuitive for some developers who are familiar with traditional looping constructs. + May have better performance due to the control it provides over iteration. * Cons: + More verbose code, which can lead to slower execution times due to increased parsing overhead. 2. **Map**: * Pros: + Concise and expressive code that is easy to read and maintain. + Often faster than `for` loops because it uses a more efficient algorithm for iteration. * Cons: + May require additional setup (e.g., understanding the context of the `map()` function) before using it effectively. **What does the benchmark result tell us?** The latest benchmark results show that: 1. **For loop**: Chrome 104 takes approximately 227,936 executions per second. 2. **Map**: Chrome 104 takes approximately 184,871 executions per second. This suggests that, on average, using `map()` is about 25% faster than using a traditional `for` loop in this specific scenario. However, it's essential to note that the actual performance difference may vary depending on other factors, such as the size and complexity of the input data, as well as any optimizations or modifications made to the code. Keep in mind that benchmarking results can be highly dependent on the specific context and environment in which they are taken.
Related benchmarks:
map vs forEach Chris
map vs forEach Chris v2b
JS Map foreach vs for of
Array fill map, vs for i loop
Array fill map, vs while loop
Comments
Confirm delete:
Do you really want to delete benchmark?