Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs some vs for..of vs for cached length different arrays no break
(version: 0)
Compare loop performance
Comparing performance of:
for vs for cached length vs some vs for..of vs foreach
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = new Array(10000); for(i=0; i<5000; i++){ a[i] = ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)); } a[5000] = 1488; for(i=5001; i<10000; i++){ a[i] = ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)); } var b = new Array(10000); for(i=0; i<5000; i++){ b[i] = ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)); } b[5000] = 1488; for(i=5001; i<10000; i++){ b[i] = ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)); } var c = new Array(10000); for(i=0; i<5000; i++){ c[i] = ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)); } c[5000] = 1488; for(i=5001; i<10000; i++){ c[i] = ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)); } var d = new Array(10000); for(i=0; i<5000; i++){ d[i] = ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)); } d[5000] = 1488; for(i=5001; i<10000; i++){ d[i] = ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)); } var e = new Array(10000); for(i=0; i<5000; i++){ e[i] = ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)); } e[5000] = 1488; for(i=5001; i<10000; i++){ e[i] = ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)); }
Tests:
for
var ahas1488 = false; for (var i = 0; i < a.length; i++) { if (a[i] === 1488) ahas1488 = true; }
for cached length
var bhas1488 = false; for (var i = 0, l=b.length; i < l; ++i) { if (b[i] === 1488) bhas1488 = true; }
some
var chas1488 = c.some(function(i) { return i === 1488; });
for..of
var dhas1488 = false; for (var i of d) { if (i === 1488) dhas1488 = true; }
foreach
var ehas1488 = false; e.forEach(function(i) { if (i === 1488) ehas1488 = true; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
for
for cached length
some
for..of
foreach
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):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Purpose** The benchmark measures the performance of different loop constructs in JavaScript: `for`, `for` with cached length, `some`, and `for...of`. The goal is to determine which loop construct is the most efficient. **Loop Constructs** 1. **`for`**: A traditional loop that increments a counter until a condition is met. 2. **`for` with cached length**: A variation of the previous loop where the length of the array is stored in a variable, reducing the number of iterations needed to check for the desired value. 3. **`some`**: A loop that checks if at least one element in an array satisfies a certain condition. 4. **`for...of`**: A modern loop that iterates over an iterable object (like an array) without needing to specify the length. **Performance Results** The latest benchmark results show the following execution counts per second for each test case: * `some`: 2844.90 * `for cached length`: 2315.93 * `foreach` ( likely a typo, but assumed to be `for...of` ): 1738.29 * `for`: 1388.83 * `for...of`: 596.42 **Performance Comparison** The results suggest that: * `some` is the fastest, possibly due to its concise syntax and optimized engine implementation. * `for cached length` is slightly faster than `foreach`, indicating that storing the array length can improve performance. * `for` is slower than all other options, likely due to its traditional loop structure. * `for...of` is the slowest, possibly because it requires additional overhead for iteration. **Implications** These results highlight the importance of choosing the right loop construct for a specific task. In general, modern loops like `some` and `for...of` can be faster than traditional loops due to optimized engine implementations. However, in cases where array length is known, caching that value can lead to improved performance. I hope this answer helps!
Related benchmarks:
for vs foreach vs some vs for..of vs for cached length non empty array
for vs foreach vs some vs for..of vs for cached length non empty array actually check
for vs foreach vs some vs for..of vs for cached length non empty array actually check no break
foreach vs for...of w/o enties() for uint8array
Comments
Confirm delete:
Do you really want to delete benchmark?