Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs forOwn
(version: 0)
Compare different ways of iterating over an object
Comparing performance of:
Array.prototype.forEach() vs _.forOwn()
Created:
7 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 anlagenStatus = Object.assign({}, _.range(10000).map((i) => ({ status: `status_${i}`, statusDateneingang: `statusDateneingang_${i}`, })));
Tests:
Array.prototype.forEach()
const anlagenStatusKeys = Object.keys(anlagenStatus); anlagenStatusKeys.forEach(function (anlagenStatusKey) { console.log(anlagenStatusKey); console.log(anlagenStatus[anlagenStatusKey].status); console.log(anlagenStatus[anlagenStatusKey].statusDateneingang); });
_.forOwn()
_.forOwn(anlagenStatus, ({ status, statusDateneingang }, systemId) => { console.log(systemId); console.log(status); console.log(statusDateneingang); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.prototype.forEach()
_.forOwn()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.forEach()
5.8 Ops/sec
_.forOwn()
5.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and benchmark data to understand what's being tested. **Benchmark Overview** The benchmark compares two different ways of iterating over an object: `Array.prototype.forEach()` and `_._forOwn()`. The goal is to determine which method is more efficient. **Options Compared** There are two options being compared: 1. **`Array.prototype.forEach()`**: This is a built-in JavaScript method that iterates over an array (or any other iterable) using a callback function. 2. **`_.forOwn()`**: This is a function from the Lodash library, which provides a way to iterate over an object using a callback function. **Pros and Cons of Each Approach** 1. **`Array.prototype.forEach()`**: * Pros: + Built-in JavaScript method, so no additional library dependencies. + Can be used with arrays or any other iterable. + Simple syntax. * Cons: + Not optimized for object iteration, which can lead to slower performance compared to specialized object iteration methods. 2. **`_.forOwn()`**: * Pros: + Optimized for object iteration, making it faster than `Array.prototype.forEach()`. + Provides a way to access both the key and value of each property in the iteration. * Cons: + Requires the Lodash library, which can add overhead due to its size and complexity. + Syntax may be unfamiliar to some developers. **Library Usage** In this benchmark, the `_.forOwn()` function is used from the Lodash library. The library provides a convenient way to iterate over objects using a callback function, making it easier to write efficient code for object iteration. **Other Considerations** * **Context**: The benchmark is run in a Chrome browser on a Linux desktop machine. * **Performance**: The `Array.prototype.forEach()` method performs slightly better than the `_.forOwn()` method, with an execution rate of approximately 5.758 executions per second compared to 5.240 executions per second. **Alternatives** Other alternatives for iterating over objects in JavaScript include: 1. **`Object.keys()` and `forEach()`**: While not optimized for object iteration like `_.forOwn()`, this approach can still be effective using the built-in `Object.keys()` method to get an array of property names, followed by a `forEach()` loop. 2. **`.every()` or `.some()`**: These methods are designed for iterating over objects and arrays, but they may not provide direct access to the key-value pairs like `_.forOwn()`. 3. **Custom iteration loops**: Developers can write their own custom iteration loops using `for` loops, `while` loops, or even recursive functions. In summary, while both methods have their pros and cons, the `Array.prototype.forEach()` method is a built-in JavaScript method that may not be optimized for object iteration, whereas the `_.forOwn()` function from Lodash provides an optimized way to iterate over objects using a callback function.
Related benchmarks:
lodash vs for-of vs forEach
lodash.each vs Object.forEach vs lodash map
lodash foreach vs for-of vs forEach
Lodash forOwn vs Native keys + forEach 1000 keys
Comments
Confirm delete:
Do you really want to delete benchmark?