Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs some vs for..of (x10000)
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs some vs for..of
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(10000);
Tests:
for
for (var i = 0; i < array.length; i++) { array[i]; }
foreach
array.forEach(function(i) { array[i]; });
some
array.some(function(i) { array[i]; });
for..of
for (var i of array) { array[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for
foreach
some
for..of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Browser/OS:
Chrome 143 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
63115.4 Ops/sec
foreach
54819.4 Ops/sec
some
47396.6 Ops/sec
for..of
13462.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! The provided benchmark is designed to compare the performance of four different loop constructs in JavaScript: `for`, `forEach`, `some`, and `for..of`. The goal is to determine which construct is the most efficient for iterating over an array. **Loop Constructs Comparison** 1. **`for` loop**: This traditional loop construct uses a manual index variable (`i`) to access elements of the array. ```javascript for (var i = 0; i < array.length; i++) { array[i]; } ``` Pros: Simple and widely supported. Cons: Can be error-prone if not used carefully, as the index variable can lead to off-by-one errors. 2. **`forEach` loop**: This construct uses a callback function to iterate over each element of the array. ```javascript array.forEach(function(i) { array[i]; }); ``` Pros: More concise and expressive than traditional `for` loops. Cons: May have performance overhead due to the extra indirection. 3. **`some` loop**: This construct uses a callback function to iterate over each element of the array, but with an additional check for termination. ```javascript array.some(function(i) { array[i]; }); ``` Pros: Similar to `forEach`, but allows for early termination if no elements match. Cons: May have performance overhead due to the extra indirection. 4. **`for..of` loop**: This modern construct uses an iterator protocol to iterate over each element of the array. ```javascript for (var i of array) { array[i]; } ``` Pros: More concise and expressive than traditional `for` loops, with improved performance due to reduced overhead. Cons: Less widely supported than other constructs. **Library and Special Features** The provided benchmark uses a custom script that creates an array of 10,000 elements before running each loop construct. There are no specific libraries or modules used in this benchmark. However, the `forEach` and `some` loops use the Array.prototype.forEach() and Array.prototype.some() methods, which are built-in JavaScript methods for iterating over arrays. **Other Considerations** The benchmark's performance results can be influenced by various factors, such as: * The specific hardware and software configurations of the test devices. * The size and distribution of the input data (in this case, an array of 10,000 elements). * Any additional overhead or optimizations enabled in the JavaScript engine. **Alternatives** If you're interested in exploring alternative loop constructs or testing other performance-related JavaScript features, some alternatives to MeasureThat.net include: * jsperf: A lightweight JavaScript benchmarking tool that allows for more fine-grained control over test configurations. * Benchmark.js: A popular JavaScript benchmarking library that provides a range of tools and APIs for building custom benchmarks. * V8.js: The official JavaScript engine used by Google Chrome, which includes various performance-related features and optimizations. I hope this explanation helps you understand the benchmark results and the different loop constructs being compared!
Related benchmarks:
for vs foreach vs some with 10k data
for vs foreach for (cached length) vs for..of
for vs foreach vs some vs for..of(100,000,000)
for (cache length) vs foreach vs for..in vs for..of
for vs foreach vs some vs for..of big (over a million runs)
Comments
Confirm delete:
Do you really want to delete benchmark?