Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
MinMax test
(version: 0)
Comparing performance of:
for vs foreach vs forin vs forof
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arrays = []; var range = {min:0,max:0}; for (let i = 0; i < 100; i++) { arrays.push({ name: 'asdf', price: Math.random() * (10000 - 1000) + 1000 }) }
Tests:
for
for (let i = 0; i < 100; i++) { const price = arrays[i].price; if (price < range.min) range.min = price; else if (price > range.max) range.max = price; }
foreach
arrays.forEach((obj)=>{ const price = obj.price; if (price < range.min) range.min = price; else if (price > range.max) range.max = price; })
forin
for (const i in arrays){ const price = arrays[i].price; if (price < range.min) range.min = price; else if (price > range.max) range.max = price; }
forof
for (const obj of arrays){ const price = obj.price; if (price < range.min) range.min = price; else if (price > range.max) range.max = price; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for
foreach
forin
forof
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
44288.1 Ops/sec
foreach
64283.0 Ops/sec
forin
34573.8 Ops/sec
forof
57333.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a JavaScript microbenchmark, specifically the MinMax test, which is designed to measure the performance of different loops in JavaScript. **Loop Comparison** The benchmark compares four types of loops: 1. **Traditional `for` loop**: `for (let i = 0; i < 100; i++) {\r\n\tconst price = arrays[i].price;\r\n \tif (price < range.min) range.min = price;\r\n \telse if (price > range.max) range.max = price;\r\n}` 2. **`forEach` loop**: `arrays.forEach((obj)=>{\r\n \tconst price = obj.price;\r\n \tif (price < range.min) range.min = price;\r\n \telse if (price > range.max) range.min = price;\r\n})` 3. **`for-in` loop**: `for (const i in arrays){\r\n \tconst price = arrays[i].price;\r\n \tif (price < range.min) range.min = price;\r\n \telse if (price > range.max) range.min = price;\r\n}` 4. **`for-of` loop**: `for (const obj of arrays){\r\n \tconst price = obj.price;\r\n \tif (price < range.min) range.min = price;\r\n \telse if (price > range.max) range.min = price;\r\n}`` **Comparison Options** The benchmark compares these four loop types based on their performance in terms of **Executions Per Second**. Pros and Cons: * **Traditional `for` loop**: * Pros: More control over indexing, less memory allocation overhead. * Cons: Can be slower due to manual index management. * **`forEach` loop**: Efficient and modern approach, but can lead to more memory allocations. * **`for-in` loop**: Less efficient than traditional `for` loops and may not work well with arrays. * **`for-of` loop**: Modern and convenient, but can be slower due to additional overhead. **Library: `forEach`** The `forEach` loop uses the built-in JavaScript method `forEach()` which iterates over an array. The callback function is invoked once for each element in the array. **Special JS Feature/Syntax: `for-of` Loop** The `for-of` loop was introduced in ECMAScript 2015 (ES6) as a new way to iterate over arrays and other iterable objects. It provides a more concise and expressive syntax compared to traditional `for` loops. **Alternatives** If you're looking for alternative approaches, consider: * **Generator functions**: Can be used to implement custom iteration logic without the need for explicit loops. * **Iterators**: Allow you to define custom iteration logic using a separate function that provides access to the iteration state. * **Array.prototype.map()`, `Array.prototype.filter()` and `Array.prototype.reduce()`: Useful methods for array transformations and aggregations, but may not be as efficient as custom loops in certain scenarios. In summary, the MinMax test benchmark allows users to compare the performance of different JavaScript loop types, providing insights into the most efficient approach for specific use cases.
Related benchmarks:
Labels
Min/max for array
ternary vs if in loop
for vs reduce (find max value) i10000
Set.has v.s Array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?