var A = Array.from({length: 40000000}, () => Math.floor(Math.random() * 140));
var B = Array.from({length: 40000000}, () => Math.floor(Math.random() * 140));
var C = A.filter((a) => !B.includes(a));
var C = [new Set(A,B)];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Filter | |
Array Set |
Test name | Executions per second |
---|---|
Filter | 0.7 Ops/sec |
Array Set | 1.6 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark is designed to compare the performance of two approaches for filtering or manipulating arrays in JavaScript: filter()
and using an array set (Set
).
Options Being Compared
There are two test cases:
filter()
method to create a new array with elements that do not match the condition specified by the callback function.A
and B
) using the spread operator (...
). The resulting set is then converted back into an array.Pros and Cons of Each Approach
Library/Functionality Used
The Set
object is a built-in JavaScript library that provides an efficient way to store unique values. In this benchmark, it's used to create a new array set from two existing arrays (A
and B
). The spread operator (...
) is used to convert the resulting set back into an array.
Special JS Feature/Syntax
The benchmark uses the spread operator (...
) which is a modern JavaScript syntax introduced in ECMAScript 2015 (ES6). It allows for creating new arrays or objects by spreading elements from existing ones. This syntax is not available in older versions of JavaScript, such as ES3 or ES4.
Other Alternatives
If you're interested in exploring alternative approaches to filtering or manipulating arrays, here are a few options:
reduce()
with an accumulator functionforEach()
and modifying the original array directlyKeep in mind that each approach has its own trade-offs in terms of performance, readability, and maintainability. The choice of approach depends on the specific use case, dataset size, and personal preference.