<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var arr = Array.from(Array(100000).keys())
_.filter(arr, o => o !== 99999);
_.without(arr, 99999);
arr.filter(o => o !== 99999);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
filter | |
without | |
array.filter |
Test name | Executions per second |
---|---|
filter | 1153.4 Ops/sec |
without | 1219.1 Ops/sec |
array.filter | 1148.5 Ops/sec |
Let's break down the benchmark and explain what's being tested, compared, and the pros and cons of each approach.
Benchmark Overview
The benchmark compares three approaches to filter an array in JavaScript:
_.filter
(Lodash)_.without
(Lodash)array.filter
(native JavaScript)Comparison of Approaches
_.filter(arr, o => o !== 99999)
Pros:
Cons:
_.without(arr, 99999)
Pros:
_.filter
, but with a different approach and syntax._.filter
due to the underlying algorithm used.Cons:
_filter
.arr.filter(o => o !== 99999)
Pros:
Cons:
Test Results
The latest benchmark results show that:
_.without
performed best with an average execution rate of 1153.4171142578125 executions per second on Firefox 119.array.filter
performed closest to _.without
, with an average execution rate of 1148.49462890625 executions per second on the same browser and device platform.The results suggest that both Lodash functions provide similar performance, while the native JavaScript array.filter
approach is slightly less efficient but still relatively fast.
Other Alternatives
For filtering arrays in JavaScript, other approaches exist:
Array.prototype.filter()
(native JavaScript): Similar to array.filter
, but with a more concise syntax.filter()
from underscore.js
: Another utility library that provides an alternative implementation of the filtering algorithm.Keep in mind that performance differences between these approaches may vary depending on specific use cases and requirements.