<!--your preparation HTML code goes here-->
/*your preparation JavaScript code goes here
To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/
async function globalMeasureThatScriptPrepareFunction() {
// This function is optional, feel free to remove it.
// await someThing();
}
const arrOrg = new Array(1000).fill(() => 'Nei');
const arr1 = arrOrg.map(() => () => 'Nei');
const arr2 = arrOrg.map(() => () => 'Nei');
const func = () => console.log('Hei!');
const set = new Set([arr1, func, arr2]);
set.delete(func);
const arrOrg = new Array(1000).fill(() => 'Nei');
const arr1 = arrOrg.map(() => () => 'Nei');
const arr2 = arrOrg.map(() => () => 'Nei');
const func = () => console.log('Hei!');
const set = [arr1, func, arr2];
const index = set.indexOf(func);
set.splice(index, 1);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Set delete | |
Array splice |
Test name | Executions per second |
---|---|
Set delete | 16948.5 Ops/sec |
Array splice | 36667.2 Ops/sec |
The benchmark provided evaluates the performance of two different methods for removing elements from a collection in JavaScript: using the Set
's delete
method versus using Array
's splice
method. The snippet of code in the benchmark tests the efficiency of these two operations when working with function references.
Set Delete:
const arrOrg = new Array(1000).fill(() => 'Nei');
const arr1 = arrOrg.map(() => () => 'Nei');
const arr2 = arrOrg.map(() => () => 'Nei');
const func = () => console.log('Hei!');
const set = new Set([...arr1, func, ...arr2]);
set.delete(func);
Set
that contains unique references from two mapped arrays of function references as well as one additional function (func
). It then removes func
using the delete
method provided by the Set
data structure.Set
maintains unique values, ensuring that each function reference is stored without duplication.delete
method has an average time complexity of O(1), making it very efficient for removal of existing elements.Set
, if you need to perform operations that involve ordering or indices, a different data structure might be more appropriate, as Set
does not maintain ordering of elements.Array Splice:
const arrOrg = new Array(1000).fill(() => 'Nei');
const arr1 = arrOrg.map(() => () => 'Nei');
const arr2 = arrOrg.map(() => () => 'Nei');
const func = () => console.log('Hei!');
const set = [...arr1, func, ...arr2];
const index = set.indexOf(func);
set.splice(index, 1);
set
, in this case) combining function references from two mapped arrays and one additional function. It locates the index of func
and removes it from the array using the splice
method.indexOf
method used here has a time complexity of O(n), as it must search through the array to find the index of the element. The subsequent splice
operation also can have a time complexity of O(n) in the worst case, as it may need to shift elements after the removal.The benchmark results indicate that:
This result suggests that, at least in this specific context with a moderate number of elements, the array-based splice
operation performed faster than deleting from a set, which is somewhat counterintuitive given the O(1) complexity of the delete
method. This may be influenced by additional overhead associated with maintaining the Set
structure or the way JavaScript engines optimize operations.
Using filter()
:
filter()
method on arrays can be effective. However, it creates a new array, which can have significant memory implications.Linked List Implementation:
Map vs. Set:
Map
instead of a Set
or an Array
can be beneficial, especially when needing to maintain relationships between data.Overall, this benchmark highlights the trade-offs in performance between different data structures and methods, encouraging developers to choose the most appropriate approach based on their specific requirements and performance expectations.