<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
window.test = [
{ id: 1, source: true },
{ id: 2, source: true },
{ id: 3, source: false }
]
_.filter(window.test, function (item) {
return item.source === false
})
window.test.filter(function (item) {
return item.source === false
})
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash filter | |
array splice |
Test name | Executions per second |
---|---|
lodash filter | 2676548.5 Ops/sec |
array splice | 6040254.5 Ops/sec |
Let's break down what's being tested in this benchmark.
Benchmark Overview
The test compares the performance of two approaches: using lodash
(a popular JavaScript utility library) and the built-in Array.prototype.filter()
method on native arrays.
Options Compared
The benchmark tests the following options:
_.filter()
function from Lodash is used to filter an array.Array.prototype.filter()
method is used to filter an array, without using any external library.Pros and Cons of Each Approach
Library Used
In this benchmark, lodash
is used for its _.filter()
function. Lodash is a utility library that provides a lot of functionality for common tasks, such as array manipulation, string manipulation, and object utilities. In this case, it's being used to simplify the filtering process by providing a concise and readable way to define the filter criteria.
Special JS Feature or Syntax
There isn't any special JavaScript feature or syntax being tested in this benchmark. The test focuses on comparing two different approaches for array filtering: using an external library versus native code.
Other Alternatives
If you don't want to use Lodash, there are other libraries and techniques that can be used for array filtering, such as:
underscore
or ramda
.map()
and some()
methods in combination.However, keep in mind that the native Array.prototype.filter() method is often the best choice for simple use cases where readability and performance are important.