<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; }, {})
_.each(obj, function(v, k) {})
_.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]; }
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash.each | |
lodash.map | |
Object.entries.map | |
vanilla for-loop w/ Object.entries |
Test name | Executions per second |
---|---|
lodash.each | 1098.2 Ops/sec |
lodash.map | 985.9 Ops/sec |
Object.entries.map | 959.2 Ops/sec |
vanilla for-loop w/ Object.entries | 808.6 Ops/sec |
Let's break down the provided JSON data and explain what's being tested in each test case.
Benchmark Definition
The provided JSON represents a JavaScript microbenchmark, which is a small piece of code designed to measure the performance of specific parts of a larger program.
In this benchmark, four different approaches are compared:
The goal is to determine which approach is faster in terms of execution time.
Options Compared
The two main options being compared are:
_.each
and _.map
for array manipulation.for...of
loops with Object.entries
to iterate over the object.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Library
In this benchmark, the Lodash library is used as a third-party dependency. Lodash provides a set of utility functions that can simplify code and improve performance in certain scenarios. The _.each
function is used for iterating over an object's values, while the _.map
function is used for transforming an array.
Special JS Feature
There is no special JavaScript feature or syntax being tested in this benchmark. However, the use of Object.entries
and for...of
loops with it highlights some interesting aspects of modern JavaScript:
Object.entries
: A relatively new feature introduced in ECMAScript 2015 (ES6), which allows iterating over an object's key-value pairs as arrays.for...of
loops: A type of loop that can be used to iterate over arrays, objects, and other iterable values.Other Alternatives
If you were to create a similar benchmark for another approach or library, some alternatives could include:
for
loops versus while
loops.Keep in mind that benchmarking results can vary depending on factors like hardware, software configurations, and test environments, so it's essential to run multiple tests to ensure accurate conclusions.