var arr = [];
var count = 100000;
for(var i = 0; i<count; i++)
{
arr.push(i);
}
const folderSet = new Set()
arr.forEach((f) => {
folderSet.add(f)
})
const result = arr.map((f) => f)
var end = [new Set(result)]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
set add | |
map |
Test name | Executions per second |
---|---|
set add | 108.8 Ops/sec |
map | 103.0 Ops/sec |
Let's break down the provided JSON and explain what is tested, compared, and the pros/cons of each approach.
Benchmark Overview
The benchmark measures the performance difference between two approaches: using a Set
data structure with the add()
method (known as "set add") versus using the map()
function followed by the spread operator (new Set()
) to achieve similar functionality (known as "map").
Script Preparation Code
The script preparation code creates an empty array arr
and populates it with 100,000 elements. This is done to ensure that both test cases have a large dataset to work with.
var arr = [];
var count = 100000;
for (var i = 0; i < count; i++) {
arr.push(i);
}
Options Compared
The two options being compared are:
add()
method of a Set
data structure to add elements to it.map()
function to transform the array into an array of values, and then creating a new Set
from this array using the spread operator (new Set()
).Pros and Cons
Pros:
Set
stores unique elements, which can lead to better cache locality and faster lookup times.Set
is typically a constant time operation.Cons:
Set
only provides basic methods like add()
, has()
, and size
. More complex operations might require converting the Set
back into an array or using external libraries.Set
can be slow due to the overhead of creating and managing the internal data structure.Pros:
map()
function allows for more complex transformations, and converting the result to a Set
provides a way to remove duplicates.Set
can be faster than repeatedly adding elements using add()
, as it avoids the overhead of managing a growing internal data structure.Cons:
Set
from an array requires additional memory allocations and copies, which can impact performance.map()
function adds an extra step, making the overall code more complicated.Library
In this benchmark, no specific library is used. However, if you're interested in exploring other approaches or optimizing your code further, you might consider using libraries like Lodash or a custom utility function for set operations.
Special JavaScript Features/Syntax
There are no special JavaScript features or syntax being tested in this benchmark. Both Set
and the map()
function are standard JavaScript constructs.
Alternatives
Other approaches to compare could include:
filter()
to remove duplicatesuniqBy()
functionKeep in mind that the choice of approach depends on your specific use case and performance requirements. This benchmark provides a general comparison of two common methods, but you may need to experiment with different approaches to find the best fit for your application.