Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Loop over object: lodash.forOwn vs Object.keys().forEach
(version: 0)
Comparing performance of:
lodash.each vs lodash.forOwn vs Object.keys().forEach vs for..in
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 = Array.from({ length: 1000 }).map((value, i) => i).reduce((val, v) => { val[v] = v; return val; }, {})
Tests:
lodash.each
_.each(obj, function(v, k) { console.log(v, k); });
lodash.forOwn
_.forOwn(obj, function(v, k) { console.log(v, k); });
Object.keys().forEach
Object.keys(obj).forEach(function (k) { let v = obj[k]; console.log(v, k); });
for..in
for (const k in obj) { if (obj.hasOwnProperty(k)) { let v = obj[k]; console.log(v, k); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
lodash.each
lodash.forOwn
Object.keys().forEach
for..in
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):
**Overview of the Benchmark** The provided JSON represents a JavaScript benchmarking test case on MeasureThat.net. The test compares the performance of different approaches for iterating over an object: `lodash.forOwn`, `Object.keys().forEach`, and `for..in`. Additionally, it uses the Lodash library to define a custom function `_each` and `_forOwn`. **Options Compared** The benchmark tests the following options: 1. **_lodash.forOwn**: A method from the Lodash library that iterates over an object using its own internal implementation. 2. **Object.keys().forEach**: A built-in JavaScript method that uses `Object.keys()` to get an array of the object's property names and then calls the provided callback function on each one, effectively iterating over the object's properties. 3. **for..in**: A traditional for loop in JavaScript that iterates over an object using its own internal implementation. **Pros and Cons of Each Approach** 1. **_lodash.forOwn**: * Pros: High-performance, concise code, easy to read and maintain. * Cons: Requires the Lodash library, which may not be included in all environments (e.g., older browsers). 2. **Object.keys().forEach**: * Pros: Standard JavaScript, widely supported across different browsers and environments. * Cons: May have performance overhead due to the additional step of getting an array of property names using `Object.keys()`. 3. **for..in**: * Pros: Standard JavaScript, no external libraries required. * Cons: Can be slower than _lodash.forOwn_ due to the overhead of checking for own properties and iterating over them. **Library Used: Lodash** Lodash is a popular utility library for JavaScript that provides various functions for tasks such as array manipulation, object iteration, and more. In this benchmark, Lodash is used to define two custom functions: 1. `_each`: Iterates over an object using its own internal implementation. 2. `_forOwn`: Iterates over an object using its own internal implementation. **Special JavaScript Feature or Syntax** None of the tested approaches use any special JavaScript features or syntax. **Alternatives** Other alternatives for iterating over objects in JavaScript include: 1. Using `Array.prototype.forEach` on an array representation of the object. 2. Using a simple loop with `in` and `hasOwnProperty`. 3. Utilizing modern ES6+ methods such as `for...of` or `reduce()`. However, these alternatives may not be suitable for this specific benchmark due to their potential performance overhead or limitations in certain edge cases.
Related benchmarks:
Loop over object: lodash vs Object.entries
Loop over object: lodash vs Object.entries and Object.keys
Loop over object: lodash vs Object.entries [2]
Loop over object: lodash vs Object.entries vs Object.keys vs Object.values
Comments
Confirm delete:
Do you really want to delete benchmark?