Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
totalQuantitiesCount
(version: 0)
Comparing performance of:
reduce vs forEach if vs for of vs for of if vs for vs forEach
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
function generateTestData(size) { const carts = {}; for (let i = 0; i < size; i++) { const cartType = `cart_${i}`; carts[cartType] = Math.random() > 0.1 ? { totalQuantityCount: Math.floor(Math.random() * 100) } : null; } return carts; } var state = { carts: { carts: generateTestData(1000000) } }; var totalQuantitiesCount = 0;
Tests:
reduce
Object.values(state.carts.carts).reduce((totalQuantitiesCount, cart) => totalQuantitiesCount + (cart?.totalQuantityCount ?? 0), 0);
forEach if
Object.values(state.carts.carts).forEach((cart) => { if (cart !== null) totalQuantitiesCount += cart.totalQuantityCount; });
for of
for (const cart of Object.values(state.carts.carts)) { totalQuantitiesCount += cart?.totalQuantityCount ?? 0; }
for of if
for (const cart of Object.values(state.carts.carts)) { if (cart !== null) totalQuantitiesCount += cart.totalQuantityCount; }
for
const carts = Object.values(state.carts.carts); for (let i = 0; i < carts.length; i++) { const cart = carts[i]; if (cart !== null) { totalQuantitiesCount += cart.totalQuantityCount; } }
forEach
Object.values(state.carts.carts).forEach((cart) => totalQuantitiesCount += (cart?.totalQuantityCount ?? 0));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
reduce
forEach if
for of
for of if
for
forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce
2.0 Ops/sec
forEach if
1.6 Ops/sec
for of
1.5 Ops/sec
for of if
1.5 Ops/sec
for
1.6 Ops/sec
forEach
1.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided benchmark tests the execution time and performance of different methods to count the total quantities in an array of objects. **What is being tested:** * The benchmark compares six different approaches to iterate over an array of objects and calculate the sum of a specific property (totalQuantityCount). * Each approach uses the `Object.values()` method to get an array of object values, which are then iterated over using either a `for` loop, a `forEach` loop, or a functional programming approach with `reduce()`. **Options compared:** 1. **For loop**: A traditional loop that iterates over the array using a counter variable. * Pros: Simple and straightforward, easy to understand. * Cons: Can be slower due to the overhead of the loop control logic. 2. **ForEach loop**: A loop that iterates over the array using an arrow function or a callback function. * Pros: Similar to the traditional for loop but with a more concise syntax. * Cons: May have performance implications due to the use of a separate function. 3. **Reduce()**: A functional programming approach that applies a reduction function to each element in the array. * Pros: Can be faster than traditional loops or foreach loops, especially for large datasets. * Cons: May require more memory and CPU cycles due to the overhead of the reduction function. 4. **For...of loop**: A loop that iterates over the array using a new type of loop that is specifically designed for iterating over arrays and objects. * Pros: Similar to traditional loops but with a more concise syntax and potentially better performance. * Cons: May have limited support in older browsers or environments. 5. **For loop with conditional statement**: A variation of the traditional loop where an if statement is used to skip certain elements. * Pros: Similar to traditional for loops but with added flexibility. * Cons: May introduce additional overhead due to the use of a conditional statement. **Other considerations:** * The benchmark uses a large dataset (1,000,000 objects) to simulate real-world performance scenarios. * The results are displayed in terms of executions per second, which provides a rough estimate of the performance difference between each approach. * The benchmark is run on Chrome 127, a relatively modern browser with good performance characteristics. **Library and special JS features:** None of the approaches rely on any specific libraries or special JavaScript features. However, it's worth noting that the use of `Object.values()` and the newer `for...of` loop syntax may be considered non-standard in older browsers or environments. **Alternatives:** * Other iterative methods could be used, such as using a `while` loop or an `Array.prototype.forEach()` with a callback function. * Alternative approaches might include using a parallel processing library or distributing the computation across multiple CPU cores. * Depending on the specific use case and requirements, other optimization techniques might be applicable, such as caching, memoization, or using a more efficient data structure.
Related benchmarks:
Fill array with random integers
Math vs tern
Division2
totalQuantitiesCount 2
Comments
Confirm delete:
Do you really want to delete benchmark?