Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for loop comparison test modernized ES6
(version: 0)
Comparing performance of:
lodash.forEach vs native vs ES5 forEach (cannot break;) vs ES5 for-in vs ES6 for-of
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script>
Script Preparation code:
var values = [{a: 30310}, {b: 100303}, {c: 3040494}];
Tests:
lodash.forEach
let count = 0; _.forEach(values, function(v,i) { if (v.a != null) { count++; } });
native
let count = 0; for (let i = 0; i < values.length; i++) { if (values[i].a != null) { count++; } }
ES5 forEach (cannot break;)
let count = 0; values.forEach(function(v,i) { if (v.a != null) { count++; } });
ES5 for-in
let count = 0; for (value in values) { if (value.a != null) { count++; } }
ES6 for-of
let count = 0; for (value of values) { if (value.a != null) { count++; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
lodash.forEach
native
ES5 forEach (cannot break;)
ES5 for-in
ES6 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 break down the benchmark and its test cases. **What is tested:** The provided JSON represents a JavaScript microbenchmark that compares different approaches to iterate over an array of objects. The benchmark tests four methods: 1. `_.forEach` from the Lodash library 2. A native for loop 3. ES5 `forEach` with a callback function (note: this has a limitation due to the way it breaks out of loops) 4. ES5 `for-in` 5. ES6 `for-of` The benchmark tests the performance of these methods on an array of objects (`values`) containing three properties (`a`, `b`, and `c`). **Options compared:** Here are the different approaches being compared: 1. **Lodash's `_forEach`**: This method is part of the Lodash library, which provides a collection of useful functions for functional programming. 2. **Native for loop**: This is a traditional way to iterate over an array using a `for` loop and an index variable (`i`). 3. **ES5 `forEach` with callback**: In ES5, `forEach` does not break out of loops like modern browsers do. Instead, it relies on the callback function to handle exceptions or break out of the loop manually. 4. **ES5 `for-in`**: This method uses the `in` keyword to iterate over the properties of an object. Note that this method is different from `forEach`, as it iterates over both numeric and symbolic property keys. 5. **ES6 `for-of`**: Introduced in modern browsers (including Chrome 106), ES6's `for-of` loop provides a way to iterate over iterable objects using a more concise syntax. **Pros and cons of each approach:** Here are some pros and cons for each method: 1. **Lodash's `_forEach`**: * Pros: Convenient, widely supported by libraries and frameworks. * Cons: Adds an extra dependency (the Lodash library). 2. **Native for loop**: * Pros: Fastest execution time, minimal dependencies. * Cons: More verbose code, requires manual index management. 3. **ES5 `forEach` with callback**: * Pros: Can be used in older browsers or environments where `for-of` is not supported. * Cons: Breaks out of loops manually using the callback function's `return` statement. 4. **ES5 `for-in`**: * Pros: Wide support across browsers and platforms, especially for legacy codebases. * Cons: Not designed for iterating over arrays; uses both numeric and symbolic property keys. 5. **ES6 `for-of`**: * Pros: Modern, concise syntax, efficient execution time. * Cons: Requires modern browsers or environments that support ES6+. **Other considerations:** * The Lodash library is included in the benchmark to make it easier for users who want to take advantage of its convenience and functionality. * The `values` array is defined using an object literal, which makes it easy to iterate over its properties. **Alternatives:** For this specific benchmark, alternative approaches might include: 1. **Using `Array.prototype.forEach()`**: This method provides similar behavior to Lodash's `_forEach`, but without the additional dependency. 2. **Implementing a custom iterator**: Users could implement their own custom iterator using a `Symbol.iterator` function or a callback function, depending on their needs. Keep in mind that the choice of approach depends on the specific use case, performance requirements, and personal preference.
Related benchmarks:
lodash .forEach vs JS forEach
Lodash foreach vs native foreach
difference2
Lodash vs Native v3,0,0
JS ForEach Tests
Comments
Confirm delete:
Do you really want to delete benchmark?