..
var ka = ['id', 'token', 'family', 'grade', 'hex'];
var k = 'token';
var fl = function(e) {
return e !== k
};
var fna_n = function() {
return ka.filter(fl)
};
var fna_a = function() {
return ka.filter(e => e !== k)
};
fna_n();
fna_a();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
fn named | |
fn anon |
Test name | Executions per second |
---|---|
fn named | 910942.9 Ops/sec |
fn anon | 1214392.4 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Definition
The benchmark is comparing two approaches to filter an array in JavaScript:
fna_n()
: This approach defines a named function fl
that takes an element as an argument and returns true
if it's not equal to the string 'token'
. The filtered array is then obtained by calling the filter()
method on the original array, passing the fl
function as the callback.fna_a()
: This approach defines an anonymous function that takes an element as an argument and returns true
if it's not equal to the string 'token'
. The filtered array is then obtained by calling the filter()
method on the original array, passing the anonymous function as the callback.Options Compared
The benchmark is comparing two options:
=>
) to define the filtering logic.Pros and Cons of Each Approach
fna_n()
:fna_a()
:Library Usage
There is no explicit library usage in this benchmark. However, the filter()
method is a built-in JavaScript array method that uses an internal implementation to perform filtering.
Special JS Features or Syntax
There are no special features or syntax used in this benchmark. The examples use standard JavaScript syntax and do not rely on any advanced features like async/await, Promises, or Web Workers.
Other Alternatives
If you're interested in exploring other options, here are a few:
map()
instead of filter()
: If the filtering logic only changes the elements in some way, using map()
might be a more efficient alternative.every()
or some()
: These methods can be used to filter arrays, but they may have different performance characteristics compared to filter()
.Keep in mind that the specific choice of implementation will depend on the requirements of your application and the trade-offs you're willing to make between readability, maintainability, and performance.