<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
var elements = [0, 1, false, 2, '', 3];
elements.filter(_ => !!_)
elements.filter(Boolean)
elements.filter(v => v != null)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
filter(_ => _) | |
filter(Boolean) | |
filter(v => v != null) |
Test name | Executions per second |
---|---|
filter(_ => _) | 34047088.0 Ops/sec |
filter(Boolean) | 39794736.0 Ops/sec |
filter(v => v != null) | 18760992.0 Ops/sec |
The benchmark defined in the JSON provided tests the performance of different approaches to filtering an array in JavaScript. Specifically, it compares three methods for filtering a list that contains various falsy values: zero (0
), false
, an empty string (''
), and undefined
. The intention is to see how efficiently each method can perform this operation.
filter(_ => !!_)
!!_
). The first !
converts the value to a boolean (truthy or falsy), and the second !
converts it back, effectively coercing the value into its boolean equivalent and filtering out falsy values.filter(Boolean)
Boolean
constructor is passed directly to filter()
, which implicitly converts values to booleans and filters out falsy values.Boolean
function, making it clear that the intent is to filter based on truthiness.filter(v => v != null)
null
or undefined
using the loose inequality operator (!=
). It will retain all other falsy values (0
, false
, ''
).null
and undefined
but keep other falsy values in the filtered results.0
, false
, or empty strings, which may not be desirable in every context. Additionally, the use of !=
introduces potential complexity due to JavaScript's type coercion.The benchmark results indicate how many times per second each filtering approach can execute, allowing for comparison of their performance in a real usage scenario:
filter(v => v != null)
showed the best performance at 53,255,440 executions per second.filter(Boolean)
followed with 38,030,824 executions per second.filter(_ => !!_)
performed least efficiently with 37,795,284 executions per second.filter(Boolean)
is generally encouraged for clearer and more understandable code.null
and undefined
should be excluded, then filter(v => v != null)
is appropriate. If all falsy values need to be excluded, filter(Boolean)
is better.Using Lodash's _.compact()
: If working with the Lodash library (which is included in the setup), you could use _.compact(elements)
, which creates an array with all falsy values removed. This is a handy method that enhances readability but may introduce a dependency on an external library.
Plain Loops: Using traditional for loops or forEach
iterations is another alternative. While it might be more verbose, it can provide more control over the filtering process (including additional logic).
In summary, the benchmark tests performance of various filtering techniques in JavaScript, each suitable for different scenarios, balancing performance, and code readability.