var myLargeArray = [];
for (var i = 0; i < 1 << 12; i++) {
myLargeArray.push(i);
}
const resultsArr = []
myLargeArray.forEach(function(element) {
if (element % 2 === 0) {
resultsArr.push(element)
}
});
myLargeArray
.map((element) => (element % 2 === 0 ? element : null))
.filter(evenNumber => (evenNumber !== null));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for loop push | |
map filter |
Test name | Executions per second |
---|---|
for loop push | 19920.5 Ops/sec |
map filter | 8533.9 Ops/sec |
Let's dive into the explanation.
Benchmark Definition JSON
The provided JSON represents a benchmark named "for loop push vs mapFilter". This benchmark is designed to compare the performance of two approaches: using a traditional for loop with array push and using the Array.prototype.map() method followed by the Array.prototype.filter() method.
Options being compared
The two options being compared are:
Pros and Cons
Library and Purpose
The Array.prototype.map() method and Array.prototype.filter() method are built-in JavaScript library functions that operate on arrays. They provide a concise way to perform operations on array elements without iterating over the entire array using traditional loops.
In this benchmark, these methods are used to filter out null values from the resulting array after applying a transformation to each element.
Special JS feature or syntax
There is no special JavaScript feature or syntax used in this benchmark. The code uses standard JavaScript concepts and built-in methods.
Other alternatives
If you wanted to test different approaches, you could consider the following alternatives:
Keep in mind that these alternative approaches may not be directly comparable to the original two options being tested (for loop + push and Map() + Filter()).