<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var data100 = _.fill(Array(100), 1).reduce((r, v, i) => (r[`k${i}`] = v, r), {});
var data1000 = _.fill(Array(1000), 1).reduce((r, v, i) => (r[`k${i}`] = v, r), {});
var sum = 0;
_.each(data100, (v, k) => sum+=v)
var sum = 0;
Object.entries(data100).forEach(([v, k]) => sum+=v)
var sum = 0;
for (let k in data100) { sum+=data100[k]; }
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash.each | |
Object.entries.forEach | |
for...in |
Test name | Executions per second |
---|---|
lodash.each | 209852.4 Ops/sec |
Object.entries.forEach | 59969.8 Ops/sec |
for...in | 88752.9 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
Benchmark Overview
The provided benchmark tests three different ways to iterate over an object in JavaScript:
each
functionforEach
method on arrays using Object.entries
for...in
loopEach test case uses the same data structure, an object with 100 or 1000 key-value pairs, initialized using Lodash's fill
and reduce
methods.
Benchmark Definitions
Let's break down each benchmark definition:
each
function:var sum = 0;
_.each(data100, (v, k) => sum += v);
This test uses Lodash's each
function to iterate over the object and accumulate the sum of its values.
Object.entries.forEach
:var sum = 0;
Object.entries(data100).forEach(([v, k]) => sum += v);
This test uses the Object.entries
method to convert the object into an array of key-value pairs and then iterates over it using the forEach
method.
for...in
loop:var sum = 0;
for (let k in data100) { sum += data100[k]; }
This test uses a traditional for...in
loop to iterate over the object's properties and accumulate the sum of its values.
Options Compared
The three options are compared in terms of their performance, which is measured by the number of executions per second.
each
function: This option relies on Lodash's optimized iteration mechanism, which uses a custom loop to iterate over the array.Object.entries.forEach
: This option uses the built-in Object.entries
method and the forEach
method on arrays. The performance of this option is likely influenced by the overhead of calling these methods.for...in
loop: This option uses a traditional loop to iterate over the object's properties, which can be slower due to the overhead of the loop and potential issues with property iteration.Pros and Cons
Here are some pros and cons for each option:
each
function:Object.entries.forEach
:for...in
loop:Library Used
In this benchmark, Lodash is used as a library to provide an optimized iteration mechanism.
Special JavaScript Features or Syntax
There are no special features or syntax used in this benchmark that would require specific knowledge of JavaScript. The code snippets provided are straightforward and easy to understand.
Alternatives
If you need to iterate over objects in JavaScript, here are some alternative approaches:
in
operator: for (let k in data100) { sum += data100[k]; }
for...of
loop or Array.prototype.forEach
Keep in mind that the performance of these alternatives may vary depending on the specific use case and requirements.