Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for of over a small array
(version: 0)
Comparing performance of:
native for of vs babel for of vs babel for of loose vs indexed for loop
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function meow() { return [1, 2, 42, 52]; }
Tests:
native for of
const foo = meow() foo.push(1337); let blah; for (let i of foo) { blah = i; }
babel for of
const foo = meow(); foo.push(1337); let blah; var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = foo[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { let i = _step.value; blah = i; } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } }
babel for of loose
var foo = meow(); foo.push(1337); let blah; for (var _iterator = foo, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { var _ref; if (_isArray) { if (_i >= _iterator.length) break; _ref = _iterator[_i++]; } else { _i = _iterator.next(); if (_i.done) break; _ref = _i.value; } var i = _ref; blah = i; }
indexed for loop
const foo = meow() foo.push(1337); let blah; for (let k = 0; k < foo.length; k++) { blah = foo[k]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
native for of
babel for of
babel for of loose
indexed for loop
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):
I'll dive into the explanation. **Overview** The provided benchmark measures the performance of three different approaches to iterate over an array: `for...of`, `var for` with `Symbol.iterator`, and `indexed for loop`. **Options compared** 1. **Native `for...of`**: This approach uses the new `for...of` syntax, introduced in ECMAScript 2015 (ES6). It allows iterating over arrays and other iterable objects without the need to define an iterator function. 2. **Babel `var for` with `Symbol.iterator`**: This approach uses the old `for` loop syntax with the `var` keyword and explicitly defines an iterator using the `Symbol.iterator` property. 3. **Indexed `for loop`**: This approach uses a traditional `for` loop with array indices to iterate over the elements of the array. **Pros and Cons** 1. **Native `for...of`**: * Pros: concise, expressive, and modern syntax; automatically handles iteration over arrays and other iterable objects. * Cons: may have slower performance due to additional overhead for parser and execution engines. 2. **Babel `var for` with `Symbol.iterator`**: * Pros: allows for fine-grained control over iteration, which can be useful in some cases; compatible with older JavaScript engines that don't support `for...of`. * Cons: requires more code to define the iterator function, which can increase maintenance overhead. 3. **Indexed `for loop`**: * Pros: highly optimized for performance, as it directly accesses array elements using indices. * Cons: less expressive and less modern syntax; requires explicit handling of array bounds and indexing. **Other considerations** 1. **Library usage**: The benchmark uses a custom JavaScript function `meow()` to create a small array with four elements. This is not relevant to the iteration approach being tested. 2. **Special JS features or syntax**: There are no special features or syntax used in this benchmark. **Benchmark interpretation** The benchmark measures the performance of each iteration approach on a desktop Chrome 79 browser with Linux operating system. The results show that: * Indexed `for loop` performs the fastest, followed by native `for...of`, and then Babel `var for` with `Symbol.iterator`. * Native `for...of` has relatively high execution rates (9275427.0/s, 8785168.0/s) on desktop Chrome. * Babel `var for` with `Symbol.iterator` performs slower than native `for...of`, but still competitive. Keep in mind that these results are specific to the benchmarked environment and may vary depending on other factors such as system load, network latency, or specific use cases.
Related benchmarks:
Loop Iteration
Array concat vs spread operator vs push for array flat implementation
for vs foreach vs some vs for..of non-empty array square root
forEach + push vs. recude + ...
testando 123 teste
Comments
Confirm delete:
Do you really want to delete benchmark?