Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash v native for/each and for of
(version: 0)
Comparing performance of:
lodash each vs native forEach vs Native for on Object.entries
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
Script Preparation code:
var obj = [{a: 3120}, {b: 133303}, {c: 3022455}, {d: 6542322}, {e: 12443521}]
Tests:
lodash each
_.each(obj, function(a,b) {console.log(a)})
native forEach
obj.forEach(function(a,b) {console.log(a)})
Native for on Object.entries
for (const [, a] of Object.entries(obj)) { return function(a,b) {console.log(a)} }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash each
native forEach
Native for on Object.entries
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 provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Context** The test is comparing three approaches to iterate over an array in JavaScript: 1. **Lodash `_.each`**: A popular utility library that provides a variety of functions for iterating over arrays. 2. **Native `forEach` loop**: The built-in `forEach` method on arrays, which uses a native implementation. 3. **Native `for...of` loop on `Object.entries`**: A newer syntax introduced in ECMAScript 2019 (ES2020) that allows iterating over the properties of an object using an array-like structure. **Benchmark Script and Preparation Code** The script preparation code defines an object `obj` with five elements, each containing a property value: ```javascript var obj = [{a: 3120}, {b: 133303}, {c: 3022455}, {d: 6542322}, {e: 12443521}]; ``` The HTML preparation code includes the Lodash library from a CDN: ```html <script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script> ``` **Benchmark Definition JSON** The benchmark definition defines three test cases: 1. **Lodash `_.each`**: Tests the `_.each` method on the `obj` array, logging each element's value to the console. 2. **Native `forEach` loop**: Tests the native `forEach` method on the `obj` array, logging each element's value to the console. 3. **Native `for...of` loop on `Object.entries`**: Tests the new syntax using `Object.entries` to create an iterator over the object's properties, then uses a `for...of` loop to iterate over the values, logging each one to the console. **Comparison** The benchmark is comparing the performance of these three approaches: * Lodash `_.each`: A third-party library method that wraps a native implementation. * Native `forEach` loop: The built-in implementation for iterating over arrays using a callback function. * Native `for...of` loop on `Object.entries`: A newer syntax introduced in ES2020 for iterating over object properties. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Lodash `_.each`**: + Pros: Easy to use, well-maintained library with extensive documentation. + Cons: Adds overhead due to the wrapper function. * **Native `forEach` loop**: + Pros: Fast, efficient implementation without additional overhead. + Cons: May not be as convenient or readable as other options. * **Native `for...of` loop on `Object.entries`**: + Pros: New syntax provides a concise and expressive way to iterate over object properties. + Cons: Limited support on older browsers (before ES2020). **Library: Lodash** Lodash is a popular utility library for JavaScript that provides various functions, including array manipulation methods like `_each`. The implementation of `_.each` likely uses a native implementation under the hood to optimize performance. **Special JS Feature or Syntax: Native `for...of` loop on `Object.entries`** The new syntax introduced in ES2020 allows iterating over object properties using an array-like structure. This syntax is designed to be more readable and concise than traditional methods, making it a great option for modern JavaScript development. In conclusion, the benchmark is testing the performance of three different approaches to iterate over an array in JavaScript: Lodash `_.each`, native `forEach` loop, and native `for...of` loop on `Object.entries`. The results will help determine which approach is fastest and most suitable for specific use cases.
Related benchmarks:
lodash for-in vs native for-in (lodash version: 4.17.10)
lodash.forOwn vs Native.forEach
lodash .forEach vs JS forEach
lodash each & lodash map & native forEach
JS ForEach Tests
Comments
Confirm delete:
Do you really want to delete benchmark?