<!--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 arr = arrOrg.map(() => () => 'Nei');
const func = () => console.log('Hei!');
const set = new Set([arr, func]);
set.delete(func);
const arrOrg = new Array(1000).fill(() => 'Nei');
const arr = arrOrg.map(() => () => 'Nei');
const func = () => console.log('Hei!');
const set = [arr, func];
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 | 22549.7 Ops/sec |
Array splice | 86333.7 Ops/sec |
The benchmark being tested compares two different approaches for removing a function reference from a collection in JavaScript: using the Array.prototype.splice
method with an array versus the Set.prototype.delete
method with a Set. Below, I will detail the implementations, pros and cons of each approach, and considerations relevant to software engineers.
Set Delete
delete
method is then used to remove the specific function.const arrOrg = new Array(1000).fill(() => 'Nei');
const arr = arrOrg.map(() => () => 'Nei');
const func = () => console.log('Hei!');
const set = new Set([...arr, func]);
set.delete(func);
Array Splice
splice
to remove it.const arrOrg = new Array(1000).fill(() => 'Nei');
const arr = arrOrg.map(() => () => 'Nei');
const func = () => console.log('Hei!');
const set = [...arr, func];
const index = set.indexOf(func);
set.splice(index, 1);
According to the latest benchmark results, the performance of the two approaches is as follows:
When choosing between these approaches, developers should consider:
Other data structures could also be considered for similar operations:
In summary, the benchmark explores the performance implications of using Set.delete
versus Array.splice
for function removal. Each approach has its trade-offs, and the decision should be made based on the specific use case, performance requirements, and coding style preferences.