<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
var obj = Array.from({ length: 10000 }).map((value, i) => i).reduce((val, v) => { val[v] = v; return val; }, {});
var arr = [Array.from(10000).keys()];
const a = [];
_.each(obj, function(v,k) {
a.push(v);
});
const a = [];
for (const [k, v] of Object.entries(obj)) {
a.push(v);
}
const a =[];
Object.entries(obj).forEach(function(v, k) {
a.push(v);
})
const a=[];
const keys = Object.keys(obj);
for (let i = 0; i < keys.length; i++) { const v = obj[keys[i]];
a.push(v);}
const a=[];
const entries = Object.entries(obj);
for (let i = 0; i < entries.length; i++) { const [k, v] = entries[i];
a.push(v);}
const a =[];
const values = Object.values(obj);
for (let i = 0; i < values.length; i++){
a.push(values[i]);}
const a =[];
Object.values(obj).forEach(function(v) {
a.push(v);
})
const a =[];
for (const prop in obj) { if (obj.hasOwnProperty(prop)) { const v = obj[prop];
a.push(v);} }
const results = arr.map(x=>x);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash.each | |
native for-of w/ entries | |
native forEach w/ entries | |
vanilla for-loop w/ Object.keys | |
vanilla for-loop w/ Object.entries | |
vanilla for-loop w/ Object.values (no keys) | |
native forEach w/ Object.values (no keys) | |
native for-in | |
map |
Test name | Executions per second |
---|---|
lodash.each | 1936.6 Ops/sec |
native for-of w/ entries | 966.7 Ops/sec |
native forEach w/ entries | 792.0 Ops/sec |
vanilla for-loop w/ Object.keys | 615.0 Ops/sec |
vanilla for-loop w/ Object.entries | 992.0 Ops/sec |
vanilla for-loop w/ Object.values (no keys) | 14435.5 Ops/sec |
native forEach w/ Object.values (no keys) | 10951.5 Ops/sec |
native for-in | 472.7 Ops/sec |
map | 9311743.0 Ops/sec |
Let's break down what's being tested in the provided JSON.
Benchmark Definition
The benchmark is comparing the performance of different approaches to iterate over an object and push values into an array:
lodash.each
for-of
with Object.entries
forEach
with Object.entries
for-loop
with Object.keys
for-loop
with Object.entries
for-loop
with Object.values
(no keys)for-in
Options Compared
The options being compared are the methods to iterate over an object and push values into an array:
lodash.each
: A utility function from the Lodash library.for-of
: A built-in iteration method that returns an iterator object.forEach
: A built-in method that calls a callback function for each value in an iterable.for-loop
with Object.keys
: Using the keys()
method to get an array of keys, and then iterating over it using a traditional for
loop.for-loop
with Object.entries
: Using the entries()
method to get an iterator object for key-value pairs, and then iterating over it using a traditional for
loop.for-loop
with Object.values
: Using the values()
method to get an iterator object for values, and then iterating over it using a traditional for
loop (without keys).Performance Comparison
The benchmark measures the execution time of each approach in terms of the number of executions per second. The results show that:
for-of
with Object.entries
is generally the fastest.forEach
with Object.values
(no keys) is also fast, but slightly slower than native for-of
.for-loop
with Object.keys
and Object.entries
are slower due to the overhead of using an iterator object or iterating over a keys array.each
function is the slowest due to its additional functionality and possibly slower iteration method.Conclusion
The results suggest that native iteration methods (for-of
, forEach
) are generally faster than vanilla loop implementations with iterator objects. Using Object.keys
or Object.entries
can introduce additional overhead, while Lodash's each
function is the slowest due to its additional functionality.