Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
blah418
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs some vs for-of vs babel for-of
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100).fill(1); window.x = 0;
Tests:
for
for (var i = 0, l = array.length; i < l; i++) { x = (x + array[i]) >>> 0; }
foreach
array.forEach(function(e) { x = (x + e) >>> 0; });
some
array.some(function(e) { x = (x + e) >>> 0; });
for-of
for (var e of array) { x = (x + e) >>> 0; }
babel for-of
function _createForOfIteratorHelper(o, allowArrayLike) { var it = (typeof Symbol !== "undefined" && o[Symbol.iterator]) || o["@@iterator"]; if (!it) { if ( Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || (allowArrayLike && o && typeof o.length === "number") ) { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError( "Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method." ); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } var _iterator = _createForOfIteratorHelper(array), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done; ) { var e = _step.value; x = (x + e) >>> 0; } } catch (err) { _iterator.e(err); } finally { _iterator.f(); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
for
foreach
some
for-of
babel for-of
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 on MeasureThat.net. **Benchmark Definition** The benchmark definition is provided as a JSON object, which defines the test case and its characteristics. In this case, the benchmark is focused on comparing the performance of different loop constructs: `for`, `foreach`, `some`, and `for-of`. **Loop Constructs** Let's break down each loop construct: 1. **`for`**: A traditional `for` loop that iterates over an array using an index variable (`i`). The loop increments `i` on each iteration, and the current element is accessed using `array[i]`. 2. **`foreach`**: An example of a `forEach` loop, which is similar to a traditional `for` loop but uses an iterator object to iterate over the array. 3. **`some`**: A loop that iterates over an array using the `some()` method, which returns `true` as soon as the condition is met. The loop only increments when the condition is true. 4. **`for-of`**: An example of a `for-of` loop, which is similar to a traditional `for` loop but uses an iterator object to iterate over the array. **Babel's `for-of`** The `for-of` loop is implemented using Babel's polyfill for modern JavaScript features. The code snippet shows how Babel generates the equivalent `for` loop code for older browsers that don't support the `for-of` syntax. **Performance Comparison** The benchmark results show the execution frequency of each loop construct in Firefox 107, measured in executions per second (ExecutionsPerSecond). The order of performance is: 1. `for`: 3156031.25 ExecutionsPerSecond 2. `babel for-of`: 1030901.0 ExecutionsPerSecond 3. `some`: 626897.1875 ExecutionsPerSecond 4. `foreach`: 1548346.75 ExecutionsPerSecond **Key Takeaways** * The traditional `for` loop is the fastest. * Babel's `for-of` polyfill is still slower than the original `for` loop but faster than `some`. * The performance difference between these loop constructs may not be significant for small loops, but can impact larger codebases. I hope this explanation helps you understand the JavaScript microbenchmark results on MeasureThat.net!
Related benchmarks:
check if variable in array has state (for vs foreach vs some vs for..of)
for i < length vs .forEach(t) vs for..of vs for t = array[i] vs for i = 0; i in array vs for i in array vs .reduce (Array)
Some benchmark >!>
Testing loops and find
Testing loops and finds
Comments
Confirm delete:
Do you really want to delete benchmark?