var master = Array(1000).fill(null).map((a, b) => b);
var a = [master];
var o = [];
master.forEach(x => { o[x] = x })
var s = new Set(master);
master.forEach(x => s.delete(x));
master.forEach(x => a.splice(a.indexOf(x), 1));
master.forEach(x => delete o[x]);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Set | |
Array | |
Object |
Test name | Executions per second |
---|---|
Set | 10558.5 Ops/sec |
Array | 2712.4 Ops/sec |
Object | 3344.0 Ops/sec |
Benchmark Explanation
The provided benchmark measures the performance of three different data structures in removing values from an array: Sets, Arrays, and Objects.
In this benchmark, a large array master
is created with 1000 elements, filled with null values, and then mapped to create a new array a
. Meanwhile, another variable o
is initialized as an empty object.
The three test cases are:
s
is created from the original array master
, and then each element is deleted using the delete
method.a
is modified by removing each element at its index using the splice
method, which shifts all subsequent elements to fill the gap.o
is modified by deleting each key-value pair where the value is equal to the corresponding element in the original array master
, but only if it exists as a key.Comparison of Approaches
Set: This approach uses the delete
method on the Set, which has an average time complexity of O(1) for removing elements.
Array: The array modification method splice
uses an average time complexity of O(n), where n is the number of elements being removed, as it involves shifting all remaining elements to fill the gap.
Object: The object modification method delete
uses an average time complexity similar to Set since it directly deletes the key-value pair without maintaining uniqueness automatically but may require extra iteration checks if values are used as keys.
Library Used
No specific library is mentioned in the benchmark setup code provided. However, JavaScript's built-in Set
data structure is utilized as part of the benchmark test cases.
Special JS Feature/Syntax
The benchmark tests the performance of methods that directly interact with built-in data structures (Set
, Array
, and Object
) without requiring any external libraries or advanced syntax features like async/await or Promises.