Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array splice vs. Set delete to arr
(version: 1)
Comparing performance of:
Set delete vs Array splice
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
/*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(); }
Tests:
Set delete
const arrOrg = new Array(3).fill(() => 'Nei'); const arr = arrOrg.map(() => () => 'Nei'); const func = () => console.log('Hei!'); const set = new Set([...arr, func]); set.delete(func); Array.from(set);
Array splice
const arrOrg = new Array(3).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);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set delete
Array splice
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
28 days ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0
Browser/OS:
Chrome 144 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set delete
1607101.4 Ops/sec
Array splice
2876060.5 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark provided in the JSON compares the performance of two different approaches for removing elements from a list: using the `splice` method on an array versus using the `delete` method on a Set. ### Summary of the Test Cases 1. **Set Delete** - **Benchmark Definition**: In this test case, a `Set` is created that contains a function and three identical arrow functions. The `delete` method is called to remove a specific function from the Set, and the final result is converted back into an array using `Array.from()`. - **Test Name**: "Set delete" 2. **Array Splice** - **Benchmark Definition**: This case creates a similar initial setup, but instead of a `Set`, it uses an array. The `indexOf` method is used to find the index of the function, and then `splice` is called to remove it from the array. - **Test Name**: "Array splice" ### Comparison of Options - **Set Delete** - **Pros**: - **Uniqueness**: A `Set` inherently prevents duplicate values, which can be advantageous when you're only interested in unique elements. - **Performance**: The `delete` operation on a Set generally has a constant-time complexity (O(1)), making it potentially faster for large collections. - **Cons**: - **Memory Overhead**: Sets may occupy more memory than arrays due to the underlying data structures used to maintain unique elements. - **Array Conversion**: If further array methods are needed, converting the Set back to an array with `Array.from()` can add an extra step. - **Array Splice** - **Pros**: - **Simplicity**: The syntax for splice is straightforward, and it directly modifies the original array. - **Direct Access**: Using `indexOf` allows straightforward retrieval to remove an item based on its value. - **Cons**: - **Performance**: `indexOf` can have a linear-time complexity (O(n)), which can be slower compared to Set operations, especially for larger arrays. - **Duplicates**: If the array contains duplicate functions, removing one of them will not be efficient with `indexOf` and may require additional logic. ### Libraries and Features This benchmark does not utilize any specialized JavaScript libraries. It mainly relies on built-in JavaScript structures (`Array` and `Set`) which are part of the ECMAScript standard. ### Other Considerations - **Further Alternatives**: Depending on the requirements, alternatives to using `Set` or arrays for element removal could include: - **Object or Map**: If associations are needed, using an object or a `Map` can also allow for fast lookups and removals. - **Functional Programming Libraries**: Libraries such as Lodash can help with array manipulations, including methods for removing items. - **Readability and Maintainability**: The choice between using a Set or an array may also depend on the readability and maintainability of the code. In some cases, the intent of the operation can be clearer when using one structure over another. ### Benchmark Results The benchmark indicates that the "Array splice" performed better, with an executions per second rate of approximately 5,526,382.5, as compared to 3,311,442.5 for the "Set delete". This performance difference suggests that for the specific case of removing items where functions are involved, the array's splice method may outperform Set operations in this instance, despite the theoretical advantages of Sets in larger or more complex datasets. It's important to consider these benchmarks in the context of actual use cases and not only depend on theoretical performance.
Related benchmarks:
reate array by lenght
Test array concat
Test array concat with larger array
new Array(length).fill().map vs Array.from({ length }, callback)
Test array ops
Array splice vs. Set delete
Array splice vs. Set delete #2
Pushing
set vs uniq
Comments
Confirm delete:
Do you really want to delete benchmark?