<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js'></script>
var n3 = 1000;
var n4 = 10000;
var n5 = 100000;
var n6 = 1000000;
var n7 = 10000000;
var array3 = [];
var array4 = [];
var array5 = [];
var array6 = [];
var array7 = [];
for(let i = 0; i < n3; i++) {
array3.push(i);
}
for(let i = 0; i < n4; i++) {
array4.push(i);
}
for(let i = 0; i < n5; i++) {
array5.push(i);
}
for(let i = 0; i < n6; i++) {
array6.push(i);
}
for(let i = 0; i < n7; i++) {
array7.push(i);
}
function filterLodash(arr) {
_.filter(arr, function (n) {
return n % 2 == 0
});
}
function filterTS(arr) {
arr.filter((n) => n % 2 == 0);
}
filterLodash(array3);
filterLodash(array4);
filterLodash(array5);
filterLodash(array6);
filterLodash(array7);
filterTS(array3);
filterTS(array4);
filterTS(array5);
filterTS(array6);
filterTS(array7);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
filter ts n=1000 | |
filter ts n=10000 | |
filter ts n=100000 | |
filter ts n=1000000 | |
filter ts n=10000000 | |
filter js n=1000 | |
filter js n=10000 | |
filter js n=100000 | |
filter js n=1000000 | |
filter js n=10000000 |
Test name | Executions per second |
---|---|
filter ts n=1000 | 383406.6 Ops/sec |
filter ts n=10000 | 42157.8 Ops/sec |
filter ts n=100000 | 3020.8 Ops/sec |
filter ts n=1000000 | 238.4 Ops/sec |
filter ts n=10000000 | 16.4 Ops/sec |
filter js n=1000 | 442925.8 Ops/sec |
filter js n=10000 | 49213.2 Ops/sec |
filter js n=100000 | 3458.8 Ops/sec |
filter js n=1000000 | 276.3 Ops/sec |
filter js n=10000000 | 19.1 Ops/sec |
Let's dive into the explanation of the benchmark.
What is being tested?
The benchmark tests two different approaches to filter an array: filterLodash
(using Lodash) and filterTS
(using JavaScript's built-in Array.prototype.filter()
method).
Options being compared
The options being compared are:
_filter()
functionArray.prototype.filter()
methodPros and Cons of each approach:
Lodash (filterLodash
):
Pros:
Cons:
JavaScript's built-in Array.prototype.filter()
method:
Pros:
Cons:
Benchmark results:
The benchmark results show that filterTS
is generally faster than filterLodash
, especially for smaller datasets. However, the performance difference decreases as the dataset size increases.
For example, on the filterjs n=1000
test:
filterTS
: 5.44 msfilterLodash
: 11.23 msAnd on the filterts n=100000
test:
filterTS
: 2.35 msfilterLodash
: 4.81 msHowever, it's essential to note that these results may vary depending on your specific use case, hardware, and software configuration.
Conclusion:
The choice between filterLodash
and filterTS
depends on your project's requirements, performance considerations, and team familiarity with the libraries. If you need a more concise and expressive solution, Lodash might be the better choice. However, if you prioritize native performance and simplicity, using JavaScript's built-in Array.prototype.filter()
method could be the way to go.