Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..in vs for..of with content and isEven
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for in vs for..of
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100).fill(0).map((_, i)=> i); function isEven(nr) { return nr%2 === 0; }
Tests:
for
for (var i = 0; i < array.length; i++) { const item = array[i]; if (isEven(item)){ continue; } console.log('Odd', item); }
foreach
array.forEach(function(item) { if (isEven(item)){ return; } console.log('Odd', item); });
for in
for (var i in array) { const item = array[i]; if (isEven(item)){ continue; } console.log('Odd', item); }
for..of
for (var item of array) { if (isEven(item)){ continue; } console.log('Odd', item); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for
foreach
for in
for..of
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/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
6192.5 Ops/sec
foreach
5694.5 Ops/sec
for in
6158.8 Ops/sec
for..of
6230.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is designed to compare the performance of four different loop constructs in JavaScript: 1. **Traditional `for` loop**: This loop uses a manual counter variable (`i`) to iterate over an array. 2. **`forEach` loop**: This loop uses the `Array.prototype.forEach()` method, which iterates over the elements of an array using a callback function. 3. **`for...in` loop**: This loop uses the `in` keyword to iterate over the properties (i.e., elements) of an object (in this case, an array). 4. **`for...of` loop with content and `isEven` condition**: This loop is similar to a traditional `for` loop but uses the `for...of` loop syntax with a content expression (`item`) and an additional condition (`if (isEven(item)){...}`). **Loop Options** Here's a brief explanation of each loop option: * **Traditional `for` loop**: This is a general-purpose loop that allows for manual control over the iteration process. Pros: flexible, allows for arbitrary termination conditions. Cons: can be error-prone and verbose. * **`forEach` loop**: This loop is designed specifically for iterating over arrays and objects. It's concise and easy to read, but it doesn't provide direct access to the array's indices or elements. Pros: concise, easy to read. Cons: limited control over iteration process. * **`for...in` loop**: This loop is designed for iterating over object properties (not recommended for arrays). It can be useful when working with complex data structures. Pros: can iterate over objects, but not suitable for arrays. Cons: less readable than other options, may lead to unexpected behavior if used with arrays. * **`for...of` loop with content and `isEven` condition**: This is a variation of the traditional `for` loop that uses the `for...of` loop syntax with an additional condition. It provides a concise way to iterate over an array while still allowing for arbitrary termination conditions. Pros: concise, flexible. Cons: may be less readable than other options. **Library Usage** The benchmark uses the `isEven` function to test if a number is even. This function is defined in the `Script Preparation Code`. **Special JavaScript Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. **Other Considerations** When choosing between these loop options, consider the trade-offs between: * Readability vs. flexibility * Conciseness vs. control over iteration process * Performance overhead (e.g., using `forEach` may have a slight performance penalty) In general, for simple array iterations, the `for...of` loop with content and condition is a good choice due to its conciseness and flexibility. For more complex scenarios or when working with objects, the traditional `for` loop or `for...in` loop might be more suitable. **Alternatives** If you're looking for alternative benchmarking tools, consider: * Benchmark.js: A popular JavaScript benchmarking library that provides a simple API for running benchmarks. * jsbench.net: Another popular benchmarking tool that allows users to write and run their own benchmarks. * Node.js's built-in `benchmark` module: A lightweight benchmarking tool that can be used to measure the performance of JavaScript code. Keep in mind that these alternatives may have different features, APIs, or requirements compared to MeasureThat.net.
Related benchmarks:
Regular for vs forEach
Array fill foreach, vs for i loop
for vs foreach vs map 2
For loop vs <Array>.forEach() vs for...of loop
Array fill map, vs for i loop
Comments
Confirm delete:
Do you really want to delete benchmark?