Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash forOwn vs Native keys and forEach
(version: 0)
Comparing performance of:
lodash forOwn vs Keys and then forEach
Created:
6 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 = { a: 1, b: 2, c: 3, }
Tests:
lodash forOwn
_.forOwn(obj, () => {});
Keys and then forEach
Object.keys(obj).forEach(() => {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash forOwn
Keys and then forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash forOwn
8665680.0 Ops/sec
Keys and then forEach
33141216.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what is being tested in the provided JSON. **Overview** The benchmark compares two approaches to iterate over object keys and execute some code: 1. Using Lodash's `forOwn` function, which iterates over an object's own enumerable property names (i.e., keys) and executes a provided callback function for each key. 2. Using the native JavaScript `Object.keys()` method to get an array of an object's own enumerable property names, and then using `forEach` to execute some code for each key. **Options compared** The two options being compared are: * Lodash's `forOwn` function * Native JavaScript `Object.keys()` + `forEach` **Pros and Cons** ### Lodash's `forOwn` Pros: * Often used in production code, especially when working with larger objects or more complex data structures. * Can be useful for iterating over an object's properties in a predictable and consistent manner. Cons: * Adds additional overhead due to the external library dependency. * May not be the most efficient approach, as it involves function calls and potentially some bookkeeping (e.g., keeping track of property names). ### Native JavaScript `Object.keys()` + `forEach` Pros: * No external dependencies or overhead. * Can be faster for simple iterations over a small number of properties. Cons: * May not work as expected when dealing with larger objects, as it only returns an array of keys and does not iterate over the object's properties directly. * Requires more manual bookkeeping to ensure each key is processed correctly (e.g., handling property names, avoiding prototype chain). **Other considerations** * Both approaches assume that the object has own enumerable property names. If the object has non-enumerable properties or uses other iteration methods, these approaches may not work as expected. * When using `forEach`, it's essential to ensure that each key is processed correctly and that no errors are thrown. **Library usage: Lodash** Lodash is a popular JavaScript utility library that provides various functions for working with arrays, objects, and more. In this benchmark, Lodash's `forOwn` function is used to iterate over an object's properties. The inclusion of Lodash in the benchmark setup allows users to compare its performance against the native JavaScript approach. **Special JS feature or syntax: None mentioned** There are no special JavaScript features or syntaxes being tested in this benchmark. **Alternative approaches** Other alternatives for iterating over object keys and executing some code include: * `for...in`: Iterates over an object's own enumerable property names, but also includes non-enumerable properties. * `Object.values()`, `Object.entries()`, or `Object.entries().forEach()`: These methods provide ways to iterate over an object's values, entries, or both. However, they may not be suitable for simple key-based iterations. Keep in mind that the choice of iteration method depends on the specific use case and requirements.
Related benchmarks:
Lodash forOwn vs Native keys + forEach
Lodash forOwn vs Native keys + forEach 2
Lodash forOwn vs native Object.values and forEach
For in vs For object.keys() vs lodash each
Comments
Confirm delete:
Do you really want to delete benchmark?