<script>
var array = ["m1c6kzws0iubt8g0zlsug14i","xalskr21dm5ke5wczew154s4i","hvbfe3uq3pmoehpilh4zpvi","ffr2btp9rihf3qce770ttke29","ymwh11qu16qwqn5vaafxhia4i","1l7rj49636ddmk3cbszitchaor","bg1njla9dvdkdlw9mbz1l9pb9","201y3ambt118dt7j7bxc6ry66r","hsgzqv2ex2j9j8znv3uzbyb9","k8tuq5ucdx1whgkdcby44pldi","2sjycnx525scxzfxfmwl7q4cxr"];
</script>
/* these functions assume that only one element matches, so they do not loop! */
function deleteBySplice (array, element) {
array.splice( array.indexOf( element ), 1 );
}
function deleteBySpliceIdx (array, element) {
array.splice( element, 1 );
}
function deleteByFilter (array, element) {
array = array.filter( el => el !== element );
}
deleteBySplice( array, "2sjycnx525scxzfxfmwl7q4cxr" );
deleteBySplice( array, "m1c6kzws0iubt8g0zlsug14i" );
deleteBySplice( array, "1l7rj49636ddmk3cbszitchaor" );
deleteBySpliceIdx( array, 10 );
deleteBySpliceIdx( array, 0 );
deleteBySpliceIdx( array, 5 );
deleteByFilter( array, "2sjycnx525scxzfxfmwl7q4cxr" );
deleteByFilter( array, "m1c6kzws0iubt8g0zlsug14i" );
deleteByFilter( array, "1l7rj49636ddmk3cbszitchaor" );
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Delete by Splice | |
Delete by Splice Index | |
Delete by Filter |
Test name | Executions per second |
---|---|
Delete by Splice | 1393692.6 Ops/sec |
Delete by Splice Index | 1504763.2 Ops/sec |
Delete by Filter | 964067.2 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Definition
The provided JSON defines a benchmark that tests three different approaches to delete an element from an array:
deleteBySplice
: uses the splice
method with indexOf
to find and remove the element.deleteBySpliceIdx
: uses the splice
method with an index to directly access the element and remove it.deleteByFilter
: uses the filter
method to create a new array with the element removed.Options Compared
The three options are compared in terms of their performance, which is measured by the number of executions per second (ExecutionsPerSecond).
Pros and Cons
Here's a brief overview of each approach:
indexOf
or findIndex
, which can be slower than other approaches for large arrays.deleteBySplice
.Library
The benchmark uses no external libraries beyond the standard JavaScript library.
Special JS Feature or Syntax
None of the test cases use any special JavaScript features or syntax that would impact their performance significantly.
Other Considerations
When choosing an approach, consider the following:
deleteBySpliceIdx
might be a good choice.deleteByFilter
might be more efficient.deleteBySplice
might be the best option.Alternatives
If you're interested in exploring other approaches or libraries that can help with array manipulation, here are a few alternatives:
Array.prototype.map()
: can be used to create a new array without modifying the original one.Array.prototype.reduce()
: can be used to remove elements from an array by accumulating a new array in reverse order.Keep in mind that each approach has its trade-offs, and the best choice ultimately depends on your specific requirements and performance constraints.