Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
totalQuantitiesCount 2
(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) } };
Tests:
reduce
var totalQuantitiesCount = 0; Object.values(state.carts.carts).reduce((totalQuantitiesCount, cart) => totalQuantitiesCount + (cart?.totalQuantityCount ?? 0), 0);
forEach if
var totalQuantitiesCount = 0; Object.values(state.carts.carts).forEach((cart) => { if (cart !== null) totalQuantitiesCount += cart.totalQuantityCount; });
for of
var totalQuantitiesCount = 0; for (const cart of Object.values(state.carts.carts)) { totalQuantitiesCount += cart?.totalQuantityCount ?? 0; }
for of if
var totalQuantitiesCount = 0; for (const cart of Object.values(state.carts.carts)) { if (cart !== null) totalQuantitiesCount += cart.totalQuantityCount; }
for
var totalQuantitiesCount = 0; 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
var totalQuantitiesCount = 0; 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
1.7 Ops/sec
forEach if
1.8 Ops/sec
for of
1.9 Ops/sec
for of if
1.8 Ops/sec
for
2.0 Ops/sec
forEach
2.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of different JavaScript loops is crucial for optimizing code. Let's break down the provided JSON and explain what each part represents, along with their pros and cons. **Benchmark Definition** The benchmark definition provides the script that will be executed to measure performance. In this case, there are two scripts: `Script Preparation Code` and `Html Preparation Code`. The first one generates test data for the benchmark, while the second one is empty. **Individual Test Cases** There are six test cases: 1. `reduce` 2. `forEach if` 3. `for of` 4. `for` 5. `forEach` 6. `reduce` (again) Each test case has a unique script that will be executed to measure performance. The scripts differ in how they iterate over the data and update the `totalQuantitiesCount` variable. **Script Explanation** Here's a brief explanation of each script: * `reduce`: This script uses the `reduce()` method to iterate over the data and accumulate the sum. + Pros: Efficient, concise, and easy to read. + Cons: May not be as intuitive for beginners due to its use of higher-order functions. * `forEach if`: This script uses the `forEach()` method to iterate over the data, but with an additional conditional statement (`if (cart !== null)`). + Pros: Easy to understand and maintain, especially for developers familiar with the `forEach()` method. + Cons: May introduce unnecessary overhead due to the condition check. * `for of`: This script uses a traditional `for` loop to iterate over the data, but with an arrow function (`cart => { ... }`) as the callback. + Pros: More explicit and readable than using higher-order functions. + Cons: May be less efficient due to the overhead of creating the arrow function. * `for`: This script uses a traditional `for` loop to iterate over the data, without any conditional statements or higher-order functions. + Pros: Simple and easy to understand. + Cons: May not be as efficient or concise as other options. * `forEach`: This script uses the `forEach()` method to iterate over the data, without any conditional statements. + Pros: Efficient and easy to read. + Cons: May not be suitable for all edge cases due to the lack of control over iteration. * `reduce` (again): This script is identical to the first one, suggesting a retest. **Library Usage** There is no explicit library usage in these scripts. However, some modern browsers may rely on libraries like V8 (for Chrome) or SpiderMonkey (for Firefox) under the hood. **Special JS Feature/Syntax** No special JavaScript features or syntax are used in these scripts, other than the use of arrow functions (`=>`) and template literals (`\r\n`). **Other Alternatives** Alternative loop implementations could be explored using: * `map()`: Instead of iterating over the data, you can create a new array with transformed values. * Regular expressions: You can use regular expressions to iterate over the data and update variables. * Custom loops: More complex logic can be implemented using custom loops that avoid built-in methods. In summary, each script has its pros and cons, and the choice of which one to use depends on the specific requirements and performance characteristics of your code.
Related benchmarks:
Fill array with random integers
Spread Operator VS Array.prototype.slice() VS Array.prototype.map()
parse float
totalQuantitiesCount
Comments
Confirm delete:
Do you really want to delete benchmark?