<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var array = new Array(100000).fill().map((v, i) => i);
var result = _.chain(array).map((v) => v + 1)
.filter((v) => v % 3 === 0)
.slice(0, 10).value();
var result = array.map((v) => v + 1)
.filter((v) => v % 3 === 0)
.slice(0, 10);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash filter then includes method | |
es6 filter then includes method |
Test name | Executions per second |
---|---|
lodash filter then includes method | 618581.1 Ops/sec |
es6 filter then includes method | 348.7 Ops/sec |
Let's dive into the details of the provided JSON benchmark.
Benchmark Overview
The benchmark compares the performance of two approaches to filtering and including elements in an array: using Lodash's filter
and includes
methods, versus using only JavaScript's native filter
method (with a custom implementation that includes checking for membership in an array).
Options Compared
The options being compared are:
filter
and includes
methods: These methods are used to filter elements based on a condition and check if an element is present in the original array, respectively.filter
method (with custom implementation): This approach uses only JavaScript's built-in filter
method to achieve similar functionality as Lodash's includes
method.Pros and Cons of Each Approach
Lodash's filter
and includes
methods
Pros:
Cons:
JavaScript's native filter
method (with custom implementation)
Pros:
Cons:
Other Considerations
When choosing between these two approaches, consider the following factors:
filter
and includes
methods might be overkill. For larger projects or performance-critical applications, the custom implementation might be a better choice.Library Used (Lodash)
Lodash is a popular JavaScript utility library that provides a collection of small, reusable functions. In this case, filter
and includes
are used as part of the Lodash chain method (_.chain
). The purpose of these methods is to provide concise and readable ways to filter and check elements in arrays.
Special JS Feature/Syntax (None)
There are no special JavaScript features or syntaxes being tested in this benchmark.