<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
var obj = Array.from({ length: 10000 }).map((value, i) => i).reduce((val, v) => { val[v] = v; return val; }, {})
_.map(obj, function(v, k) {})
Object.entries(obj).map(function([k, v]) {})
const entries = Object.entries(obj);
for (let i = 0; i < entries.length; i++) { const [k, v] = entries[i]; }
_.forEach(obj, function(v, k) {})
Object.values(obj).forEach(function(val, index) {
const v = val;
});
Object.keys(obj).forEach(function(key, index) {
const k = key;
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash.map | |
Object.entries.map | |
vanilla for-loop w/ Object.entries | |
lodash.forEach | |
Object.values | |
Object.keys |
Test name | Executions per second |
---|---|
lodash.map | 6670.7 Ops/sec |
Object.entries.map | 3341.4 Ops/sec |
vanilla for-loop w/ Object.entries | 3924.9 Ops/sec |
lodash.forEach | 6752.5 Ops/sec |
Object.values | 45547.2 Ops/sec |
Object.keys | 13954.2 Ops/sec |
Let's break down what's being tested on MeasureThat.net.
Benchmark Definition
The benchmark is designed to compare the performance of different approaches for iterating over an object in JavaScript:
lodash
(Lodash) methods: map
, forEach
Object.entries
with a for
loop, Object.values
with a forEach
loop, and Object.keys
with a forEach
loopOptions being compared
Each option is being tested for its performance using the following metrics:
The options being compared are:
lodash.map
lodash.forEach
Object.entries.map
Object.values.forEach
Object.keys.forEach
Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Library usage
In this benchmark, the lodash
library is being used to provide a set of utility functions that are commonly used in JavaScript development.
The lodash
methods (map
, forEach
) use closures to iterate over the object, which can introduce additional overhead. The vanilla JavaScript methods (using Object.entries
, Object.values
, and Object.keys
) do not rely on closures, making them potentially more efficient.
Special JS features or syntax
There are no special JavaScript features or syntaxes being used in this benchmark, such as async/await, promises, or ES6+ classes.
Other alternatives
Some other alternatives to Lodash for iterating over objects could include:
Array.prototype.forEach
Array.prototype.map
for
loops or while
loopsOverall, the benchmark is designed to help developers understand the performance implications of different approaches when iterating over objects in JavaScript.