<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js'></script>
var value = [{a: 30310}, {b: 100303}, {c: 3040494}, {d: 304045684}, {e: 38846325040494}]
_.forEach(value, function(v,i) {})
value.forEach(function(v,i) {})
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash _.forEach() | |
Native |
Test name | Executions per second |
---|---|
Lodash _.forEach() | 14394269.0 Ops/sec |
Native | 71175104.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares the performance of two approaches:
forEach
: The native forEach
method built into JavaScript, which is used to iterate over an array or iterable object without explicitly iterating through its elements.Benchmark Script
The script preparation code defines a simple JavaScript array value
with five elements, each containing an object with a single property (e.g., {a: 30310}
). This array will be used as the input data for both benchmark tests.
Benchmark Preparation Code
The HTML preparation code includes a link to load the Lodash library in the browser. This is necessary because the Lodash _.forEach` function relies on this external library.
Comparison of Approaches
Both approaches iterate over the value
array, but they do so differently:
forEach
: The native forEach
method executes a callback function for each element in the array, providing both the value (v
) and index (i
) as arguments. This approach is lightweight and has good performance characteristics.**: Lodash provides its own implementation of the
forEach` method, which also iterates over an array or iterable object. The main difference is that Lodash's implementation may have additional overhead due to its library functionality.Pros and Cons
forEach
:forEach
.Library: Lodash
The Lodash library is a popular JavaScript utility library that provides a collection of functions for various tasks, such as:
forEach
, map
, reduce
)Lodash's purpose is to provide a set of reusable functions that can simplify development and make code more efficient.
JavaScript Features/ Syntax
There are no special JavaScript features or syntax mentioned in this benchmark. The focus is solely on the performance comparison between native forEach
and Lodash's implementation.
Alternatives
If you're interested in exploring alternative approaches to testing performance, consider:
forEach
)Keep in mind that these alternatives might alter the benchmark results or require additional modifications to the script and preparation code.