<script src='https://raw.githubusercontent.com/lodash/lodash/4.17.15-npm/lodash.js'></script>
var value = [{a: 30310}, {b: 100303}, {c: 3040494}, {d: 6542321}, {e: 13123531}]
_.each(value, function(v,i) {console.log(v)})
value.forEach(function(v,i) {console.log(v)})
for (let v of value) { console.log(v); }
for (let i = 0; i < value.length; i++) { console.log(value[i]); }
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash | |
native | |
vanilla for-of | |
vanilla loop |
Test name | Executions per second |
---|---|
lodash | 16158.8 Ops/sec |
native | 16271.0 Ops/sec |
vanilla for-of | 19401.2 Ops/sec |
vanilla loop | 18796.4 Ops/sec |
Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark definition, which tests different approaches for iterating over an array of objects.
Benchmark Definition
The benchmark definition includes:
value
) as an array of five objects.Individual Test Cases
There are four test cases, each with a different approach for iterating over the value
array:
for...of
loop syntax to iterate over the array.for
loop with an index variable (i
) to iterate over the array.forEach
method to iterate over the array.each
function to iterate over the array.Options Comparison
The four test cases compare different approaches for iterating over arrays in JavaScript:
forEach
method and vanilla for-of loop are likely to be more efficient than the traditional for
loop, as they avoid the overhead of incrementing an index variable.each
function is likely to be slower than native methods and vanilla for-of loops, due to its additional overhead.Pros and Cons
Here are some pros and cons of each approach:
Library
The Lodash library is used in the benchmark definition. Lodash provides a set of functional programming utilities, including each
, which can be used to iterate over arrays.
JavaScript Features
The test cases use some special JavaScript features:
In summary, the benchmark definition compares different approaches for iterating over arrays in JavaScript. The results suggest that vanilla for-of loops and native forEach
methods are likely to be more efficient than traditional for
loops or Lodash's each
function.