<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var array = ['a', undefined, 'b', 1, undefined, 2, 3, undefined, undefined]
_.filter(array, data => {
return data !== undefined && data !== null
});
array.filter(data => {
return data !== undefined && data !== null
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash.filter | |
array filter |
Test name | Executions per second |
---|---|
lodash.filter | 640217.5 Ops/sec |
array filter | 796025.1 Ops/sec |
Let's dive into the benchmark.
What is tested:
The provided JSON represents a JavaScript microbenchmark that compares the performance of two approaches to filter out undefined
and null
values from an array:
filter()
function: The first test case uses Lodash, a popular utility library for JavaScript.filter()
function: The second test case tests the native JavaScript implementation of the filter()
function.Options compared:
The benchmark compares two options:
A) Using the filter()
function provided by Lodash (_.filter()
).
B) Implementing a custom filter using only standard JavaScript features (array.filter()
).
Pros and Cons:
Lodash's .filter()
function:
Pros:
filter()
function abstracts away the implementation details, making it easier to write concise code.Cons:
Native JavaScript filter()
function:
Pros:
Cons:
Library (Lodash):
Lodash is a popular utility library for JavaScript that provides a wide range of functions for tasks like filtering arrays. The filter()
function is just one of many utility functions available in Lodash. Other benefits of using Lodash include:
Special JS feature or syntax:
There is no mention of any special JavaScript features or syntax in the provided benchmark. The tests only rely on standard JavaScript features and libraries (Lodash).
Other alternatives:
If you're interested in exploring alternative approaches, here are some options:
underscore.js
or ramda
.fastify-filter
or lodash-es
.Array.prototype.filter()
or Array.prototype.every()
.Keep in mind that the choice of approach depends on your specific use case, performance requirements, and personal preferences.