Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Ahad Loops Test3
(version: 2)
Comparing performance of:
For vs While vs For of vs For each vs Reduce vs Map
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var data = window.data = []; var TOTAL_NUMBERS = window.TOTAL_NUMBERS = 100000; function getRandomInt(max) { return Math.floor(Math.random() * max); } while (data.length < TOTAL_NUMBERS) { data.push(getRandomInt(20)); }
Tests:
For
var TOTAL_NUMBERS = window.TOTAL_NUMBERS; var data = window.data; var sum = 0; let i = 0; for (; i < TOTAL_NUMBERS; i++) { sum += data[i]; }
While
var TOTAL_NUMBERS = window.TOTAL_NUMBERS; var data = window.data; var sum = 0; var i = 0; while (i < TOTAL_NUMBERS) { sum += data[i]; i++; }
For of
var TOTAL_NUMBERS = window.TOTAL_NUMBERS; var data = window.data; var sum = 0; for (let i of data) { sum += data[i]; }
For each
var TOTAL_NUMBERS = window.TOTAL_NUMBERS; var data = window.data; var sum = 0; data.forEach((i) => sum +=i);
Reduce
var TOTAL_NUMBERS = window.TOTAL_NUMBERS; var data = window.data; var sum = data.reduce((prev, current) => prev + current);
Map
var TOTAL_NUMBERS = window.TOTAL_NUMBERS; var data = window.data; var sum = data.map((i) => sum += i);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
For
While
For of
For each
Reduce
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):
**Benchmark Overview** The provided benchmark is designed to compare the performance of different JavaScript loop constructs: traditional `for`, `while`, `for...of`, and array methods (`forEach`, `map`, and `reduce`). The test case creates an array of 100,000 random integers and measures how long it takes to sum up all the numbers using each loop construct. **Loop Constructs Compared** 1. **Traditional `for` loop**: This is a traditional loop that uses a counter variable (`i`) and increments it at the end of each iteration. 2. **While loop**: This loop continues as long as a certain condition is true, which is checked at the beginning of each iteration. 3. **For...of loop**: This loop iterates over an array using a special syntax that allows you to iterate over its elements without needing a counter variable. 4. **Array methods**: * `forEach`: Iterates over an array and executes a callback function for each element. * `map`: Returns a new array with the results of applying a transformation function to each element in the original array. * `reduce`: Reduces an array to a single value by applying a reduction function to each element. **Pros and Cons** 1. **Traditional `for` loop**: * Pros: Easy to understand, efficient, and flexible. * Cons: Can be verbose and prone to errors if not used correctly. 2. **While loop**: * Pros: Can be more flexible than traditional `for` loops, but can be harder to read. * Cons: Requires careful management of the loop condition and incrementing the counter variable. 3. **For...of loop**: * Pros: Concise and easy to understand, especially for iterating over arrays. * Cons: Limited flexibility compared to traditional `for` loops or while loops. 4. **Array methods**: * Pros: Can be more concise and expressive than traditional loops, but can also be less efficient due to function calls and array traversals. * Cons: May incur additional overhead due to function calls and memory allocations. **Library Use** The benchmark uses the `window.data` and `window.TOTAL_NUMBERS` variables to store and initialize the data. These are likely global variables, but their purpose is not explicitly stated in the benchmark definition. It appears that they are used to create a shared state between different loop constructs, allowing the test to compare performance across different loops without modifying the code. **Special JS Features** The benchmark does not explicitly use any special JavaScript features beyond the standard syntax for the loop constructs mentioned above. However, it's worth noting that the `window` object and global variables like `TOTAL_NUMBERS` are not typically used in modern JavaScript development due to potential issues with scope and namespace pollution. **Alternative Approaches** Other approaches to comparing loop performance might include: 1. **Using a more sophisticated data structure**: Instead of using an array, the test could use a linked list or other data structures that require different algorithms for iteration. 2. **Adding additional operations**: The test could include additional operations within each loop construct, such as sorting or filtering, to make it harder to optimize and compare performance. 3. **Using a more efficient benchmarking framework**: There are specialized frameworks like BenchmarkJS or jsPerf that provide more features and better support for complex benchmarks. Overall, the provided benchmark provides a simple yet effective way to compare the performance of different JavaScript loop constructs.
Related benchmarks:
Lodash max vs Math.max (lodash 4.7.11)
ramda lodash sortBy
Lodash max vs JS Math.max (2022)
_.max vs Math.max
gigi becali's test
Comments
Confirm delete:
Do you really want to delete benchmark?