Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.keys -> native forEach vs lodash _.forEach
(version: 0)
Comparing performance of:
lodash foreach vs native with forEach
Created:
6 years ago
by:
Guest
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 value = {a: 30310, b: '100303', c: '3040494'}
Tests:
lodash foreach
_.forEach(value, function(key){ console.log(key); });
native with forEach
var arr = [] Object.keys(value).forEach( function(key){ console.log(key); } )
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash foreach
native with forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash foreach
109766.3 Ops/sec
native with forEach
131916.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the test cases and compare different approaches to measuring performance. **Benchmark Overview** The benchmark compares two approaches to iterate over an object's keys: using `Object.keys()` with `forEach` (native approach) versus using Lodash's `_forEach` function. **Native Approach (Object.keys -> native forEach)** * Uses `Object.keys()` to get an array of the object's key names. * Iterates over this array using a `forEach` loop, which is built into JavaScript. * The script preparation code creates an object `value` with properties `a`, `b`, and `c`. * The test name is "native with forEach". **Lodash Approach (lodash _.forEach)** * Uses the Lodash library to get an array of key names using `_keys` function. * Iterates over this array using Lodash's `_forEach` function, which is a higher-order function that takes a callback function as an argument. * The script preparation code creates an object `value` with properties `a`, `b`, and `c`. * The test name is "lodash foreach". **Comparison** The main difference between the two approaches is how they handle iteration. The native approach uses a built-in JavaScript function (`forEach`) that is relatively lightweight, while the Lodash approach uses a higher-order function that adds overhead due to the need for a callback function. **Pros and Cons** Native Approach: Pros: * Lightweight and fast * Built-in JavaScript function, which is widely supported Cons: * Requires `Object.keys()` and `forEach`, which may not be as efficient as other methods Lodash Approach: Pros: * Provides more control over iteration using a callback function * Can be more flexible for certain use cases Cons: * Adds overhead due to the need for a higher-order function and callback * Requires Lodash library, which may add latency **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, object iteration, and more. The `_keys` function used in this benchmark gets an array of key names from an object, while the `_forEach` function iterates over an array using a callback function. **Special JS Feature: none** This benchmark does not use any special JavaScript features or syntax that would affect its performance. **Other Alternatives** * Other approaches to iterate over an object's keys include: + Using `for...in` loop, which can be slower than `forEach` + Using a custom iteration function with `Array.prototype.forEach.call()` + Using other libraries like underscore.js or moment.js In summary, the benchmark compares two approaches to measure performance: using native JavaScript functions (`Object.keys()` and `forEach`) versus using Lodash's higher-order functions. The choice of approach depends on the specific requirements of the application and the desired level of control over iteration.
Related benchmarks:
Lodash foreach vs native foreach
lodash .foreach with object vs native foreach on keys
lodash .foreach vs native foreach vs native forof
lodash .foreach vs native foreach vs native for loop
Comments
Confirm delete:
Do you really want to delete benchmark?