..
var ka = ['id', 'token', 'family', 'grade', 'hex'];
var k = 'token';
var fl = function(e) {
return e !== k
};
ka.filter(fl);
ka.filter(e => e !== k);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
fn named | |
fn anon |
Test name | Executions per second |
---|---|
fn named | 2126580.2 Ops/sec |
fn anon | 2441566.5 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The provided benchmark compares two approaches to filter an array in JavaScript: using a named function (fl
) vs using an anonymous function (an arrow function with a single line).
Test Cases
There are two test cases:
fn named
: This test case uses the named function fl(e)
to filter the array ka
. The function is defined as follows:var fl = function(e) {
return e !== k;
}
fn anon
: This test case uses an anonymous arrow function to filter the same array ka
. The function is defined as follows:ka.filter(e => e !== k);
Library
In this benchmark, the filter()
method of arrays is used in both test cases. The filter()
method takes a callback function as its second argument and calls it for each element in the array.
Pros and Cons of Named Function vs Anonymous Function
Using a named function (fl
) has some advantages:
However, using named functions also has some disadvantages:
Using anonymous arrow functions, on the other hand, has some advantages:
However, using anonymous functions also has some disadvantages:
Other Alternatives
There are other ways to filter an array in JavaScript, such as using map()
and every()
, or using a library like Lodash. However, these alternatives are not being compared in this benchmark.
In summary, this benchmark compares two approaches to filtering arrays: using named functions vs using anonymous arrow functions. The results suggest that the anonymous function approach is faster, but may be less readable for some developers.