<script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script>
var arrayValue = [{a: 30310}, {b: 100303}, {c: 3040494}]
var objectValue = { a: 30310, b: 100303, c: 3040494}
_.forOwn(objectValue, function(v,i) {})
for(var k in objectValue) {
var v = objectValue[k];
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash.each | |
native |
Test name | Executions per second |
---|---|
lodash.each | 4767141.5 Ops/sec |
native | 3317428.8 Ops/sec |
Let's break down the provided benchmark and explain what is being tested.
Benchmark Overview
The benchmark compares two approaches for iterating over an object: _.forOwn
from Lodash and the native for...in
loop. The test case uses two JavaScript objects, arrayValue
and objectValue
, which contain similar properties with different values.
Options Compared
Two options are being compared:
_forOwn
method: This is a utility function that iterates over an object using a provided callback function. It's designed to iterate over the keys of the object, not its values.for...in
loop: This is a built-in JavaScript syntax for iterating over the properties of an object.Pros and Cons
Lodash's _forOwn
method
Pros:
Cons:
Native for...in
loop
Pros:
Cons:
_forOwn
method for some scenarios (more on this later)Other Considerations
The test case uses _.forOwn
and for...in
loops to iterate over an object. The Lodash library is included in the HTML preparation code, making it a self-contained test.
Special JS Features or Syntax
There are no special JavaScript features or syntax used in this benchmark that require explanation.
Library Used
Lodash (specifically, its forOwn
method) is used in this benchmark. The purpose of Lodash is to provide a set of utility functions that make common programming tasks easier. In this case, the _forOwn
method provides a convenient way to iterate over objects without worrying about key order.
Benchmark Test Case
The test cases are:
lodash.each
: This test case uses the Lodash _forOwn
method to iterate over an object.native
: This test case uses the native for...in
loop to iterate over an object.Alternative Approaches
Other alternative approaches for iterating over objects include:
Object.keys()
and forEach()
for...of
loop (not applicable in this benchmark, as it's using for...in
)loop-queue
or async-map
Keep in mind that the best approach depends on the specific use case and performance requirements.