Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
object key iteration - lodash vs native custom implementation
(version: 0)
the custom code is basically what happens in lodash already - perhaps it's faster because lodash each has overhead deciding if something is an array or object
Comparing performance of:
lodash.forEach vs custom foreach
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script>
Script Preparation code:
var values = {a: 123, b: 234, c: 445, d: 23, e: 234, f:234, g: 345, h: 34, i: 43}; var forEachObjectKey = (object, toRun) => { const keys = Object.keys(object) let length = keys.length let index = -1 while (length--) { index++ toRun(keys[index], (object)[keys[index]]) } }
Tests:
lodash.forEach
let count = 0; _.forEach(values, (v,i) => { if (i != null) { count+= i; } })
custom foreach
let count = 0 forEachObjectKey(values, (key, value) => { if (value != null) { count += value } })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash.forEach
custom foreach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0
Browser/OS:
Chrome 140 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash.forEach
6004991.5 Ops/sec
custom foreach
11798779.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into explaining the provided benchmark. **Benchmark Overview** The provided benchmark compares two approaches to iterating over object keys: using Lodash's `forEach` function and a custom implementation. **Comparison Options** The benchmark tests two options: 1. **Lodash's `forEach` function**: This option uses Lodash's `forEach` function, which is a utility function that iterates over arrays or objects. 2. **Custom Implementation**: This option implements the iteration logic manually using JavaScript features such as `Object.keys()`, `length`, and `while` loops. **Pros and Cons** Here are some pros and cons of each approach: * **Lodash's `forEach` function**: + Pros: - Leveraging Lodash's optimized implementation, which may reduce overhead. - Easier to read and maintain, as the logic is encapsulated in a reusable function. + Cons: - Adds dependency on an external library (Lodash). - May introduce additional overhead due to the overhead of calling a function. * **Custom Implementation**: + Pros: - No dependencies on external libraries. - Can be optimized for specific use cases or hardware architectures. + Cons: - Requires manual implementation and maintenance, which can increase complexity. - May not be as efficient as Lodash's optimized implementation. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string manipulation, object manipulation, and more. The `forEach` function in this benchmark is part of the Lodash library, which iterates over arrays or objects and executes a provided callback function on each element. **Special JS Feature: Async/await** The custom implementation uses async/await syntax to handle the iteration logic. This allows for cleaner and easier-to-read code, but may also introduce additional complexity due to the use of asynchronous programming. **Benchmark Preparation Code** The benchmark preparation code defines two variables: `values` (an object with string keys) and `forEachObjectKey` (the custom implementation function). The `forEachObjectKey` function iterates over the object's keys using `Object.keys()` and executes a callback function on each key-value pair. **Latest Benchmark Result** The latest benchmark result shows that the custom implementation outperforms Lodash's `forEach` function, with approximately 290% more executions per second. However, it's essential to note that this may be due to various factors such as the specific hardware architecture, browser version, and testing environment used in the benchmark. **Other Alternatives** If you're looking for alternatives to Lodash or custom implementations, here are a few options: * **V8's built-in `Array.forEach()`**: Modern JavaScript engines like V8 (used by Chrome) provide an optimized implementation of `forEach()` that can be used as an alternative. * **Standardized array and object iteration methods**: Many modern browsers and Node.js versions support standardized iteration methods such as `Array.prototype.forEach()` or `Object.keys()`, which can be used as alternatives to Lodash's `forEach` function.
Related benchmarks:
lodash.mapKeys vs Iterating over Object.keys
Loop over object: lodash vs Object.entries vs Object.keys vs Object.values
Iterating Lodash Entries vs Object.entries (10,000 entries)
lodash mapValues vs vanilla Object.keys foreach vs lodash reduce
Loop over object: lodash.forOwn vs Object.keys().forEach
Comments
Confirm delete:
Do you really want to delete benchmark?