<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
numbers = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
];
let acc = 0;
_.forEach(numbers, function(value) {
acc += value;
});
let acc = 0;
numbers.forEach(function(value) {
acc += value;
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.forEach | |
Javascript Native forEach |
Test name | Executions per second |
---|---|
_.forEach | 1305764.5 Ops/sec |
Javascript Native forEach | 3241046.2 Ops/sec |
Let's dive into the world of JavaScript benchmarks.
Benchmark Purpose
The MeasureThat.net website allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark test case that compares two approaches: native forEach
(JavaScript) vs .forEach
(Lodash).
Native forEach
Approach
In this approach, the test uses the built-in forEach
method of the Array.prototype
in JavaScript. This method iterates over an array and executes a provided callback function for each element.
.forEach
(Lodash) Approach
The test also compares the .forEach
method from the Lodash library, which is a popular utility library for functional programming in JavaScript. The Lodash .forEach
method provides additional functionality beyond the native forEach
method.
Options Compared
Two options are compared:
forEach
(JavaScript): This approach uses the built-in forEach
method of the Array.prototype
..forEach
(Lodash): This approach uses the .forEach
method from the Lodash library.Pros and Cons of Each Approach
Here's a brief summary:
forEach
(JavaScript).forEach
(Lodash)Library: Lodash
Lodash is a popular JavaScript utility library that provides a comprehensive set of functional programming helpers, including array methods like forEach
. The library aims to provide a more expressive and efficient way of working with data in JavaScript applications.
Special JS Features/Syntax (Not applicable)
In this benchmark, no special JavaScript features or syntax are utilized. The test cases rely solely on standard JavaScript constructs and Lodash's .forEach
method.
Other Alternatives
If you're looking for alternative libraries to Lodash, some popular options include:
These alternatives may offer different trade-offs in terms of performance, syntax complexity, and feature set. However, for many use cases, the choice between these libraries will depend on personal preference, project requirements, and existing tooling infrastructure.