Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash forOwn vs Native keys + forEach 1000 keys
(version: 0)
Comparing performance of:
lodash forOwn vs Keys and then forEach vs for key vs for key value
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var obj = Object.assign({}, _.range(1000).map((i) => ({ status: `status_${i}`, statusDateneingang: `statusDateneingang_${i}`, })));
Tests:
lodash forOwn
_.forOwn(obj, (a) => {console.log(a)});
Keys and then forEach
Object.keys(obj).forEach((a) => {console.log(a)});
for key
for (let key in obj) { // check if the property/key is defined in the object itself, not in parent if (obj.hasOwnProperty(key)) { console.log(key, obj[key]); } }
for key value
for (const [key, value] of Object.entries(obj)) { console.log(key, value); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
lodash forOwn
Keys and then forEach
for key
for key value
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 JSON and explain what's being tested, compared, and some considerations. **Benchmark Definition** The benchmark is defined by a JavaScript function that creates an object with 1000 properties using Lodash's `range` function and then defines three different ways to iterate over this object: 1. **Lodash forOwn**: Uses the `forOwn` method from Lodash to iterate over the object. 2. **Native keys + forEach**: Uses native JavaScript methods (`Object.keys()` and `forEach`) to iterate over the object. 3. **for key**: Iterates over the object using a traditional `for...in` loop, checking if each property is directly defined on the object (not inherited from a parent). 4. **for key value**: Iterates over an array of key-value pairs created using `Object.entries()`. **Libraries and Special Features** * Lodash: A popular JavaScript utility library providing functional programming helpers. In this benchmark, it's used to provide the `forOwn` method. * Firefox/112: The specific browser being tested, with its version (Firefox 112) and platform (Desktop, Linux). **Comparison Options** The test compares four different approaches: 1. **Lodash forOwn**: Uses Lodash's optimized iteration method (`forOwn`) to iterate over the object. 2. **Native keys + forEach**: Utilizes native JavaScript methods (`Object.keys()` and `forEach`) to iterate over the object. 3. **for key**: Employs a traditional `for...in` loop with property checks to iterate over the object. 4. **for key value**: Uses `Object.entries()` to create an array of key-value pairs, then iterates over it. **Pros and Cons** Here's a brief summary of each approach: 1. **Lodash forOwn**: * Pros: Optimized for performance, minimal overhead. * Cons: Requires Lodash library inclusion. 2. **Native keys + forEach**: * Pros: No additional libraries required, straightforward implementation. * Cons: May incur slower overhead due to native method calls. 3. **for key**: * Pros: Simple and lightweight, no additional libraries needed. * Cons: Performance may be slower due to property checks in each iteration. 4. **for key value**: * Pros: Efficient memory usage, as it only creates an array of key-value pairs once. * Cons: May incur slightly higher overhead due to `Object.entries()` method calls. **Other Alternatives** If you were to modify the benchmark, you could consider adding additional options, such as: 1. Using `for...of` loops instead of traditional `for` loops. 2. Employing other iteration methods like `forEach` with a callback or array destructuring (`[key, value] = obj[key];`). 3. Comparing performance with different types of objects (e.g., sparse arrays vs. plain objects). Keep in mind that this benchmark is designed to compare the relative performance of four specific iteration methods, making it a straightforward comparison rather than an exhaustive exploration of all possible JavaScript iteration approaches.
Related benchmarks:
lodash.mapKeys vs Iterating over Object.keys
lodash for-in vs native for-in (lodash version: 4.17.10)
Loop over object: lodash vs Object.entries vs Object.keys vs Object.values
Loop over object: lodash.forOwn vs Object.keys().forEach
Comments
Confirm delete:
Do you really want to delete benchmark?