Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array indexOf and splice vs Set delete
(version: 0)
Comparing performance of:
indexOf arr10 vs delete set10 vs indexOf arr30 vs delete set30 vs indexOf arr99 vs delete set100
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function arrayWithLength(length) { return Array(length).fill().map((_, i) => i); } var arr10 = arrayWithLength(10); var arr30 = arrayWithLength(30); var arr100 = arrayWithLength(100); var set10 = new Set(arr10); var set30 = new Set(arr30); var set100 = new Set(arr100);
Tests:
indexOf arr10
const i = arr10.indexOf(9); arr10.splice(i, 1)
delete set10
set10.delete(9);
indexOf arr30
const i = arr10.indexOf(29); arr10.splice(i, 1)
delete set30
set10.delete(29);
indexOf arr99
const i = arr10.indexOf(99); arr10.splice(i, 1)
delete set100
set10.delete(99);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
indexOf arr10
delete set10
indexOf arr30
delete set30
indexOf arr99
delete set100
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark is measuring the performance difference between using `Array.prototype.indexOf()` and `splice()` for removing elements, compared to using a `Set` data structure for similar operations. The script creates three arrays of different lengths (10, 30, and 100) and three sets from these arrays. **Options being compared** There are two main options being compared: 1. **Array.prototype.indexOf() + Array.prototype.splice()**: This approach uses the `indexOf()` method to find the index of the element to be removed, and then uses the `splice()` method to remove the element at that index. 2. **Set.delete()**: This approach uses a built-in `Set` data structure, which is designed for fast membership testing and removal of elements. **Pros and Cons** * **Array.prototype.indexOf() + Array.prototype.splice()**: + Pros: Simple and widely supported; works well with existing code that uses `indexOf()` and `splice()`. + Cons: Can be slower than using a `Set` data structure, especially for large datasets. * **Set.delete()**: + Pros: Fast and efficient, especially for membership testing and removal of elements. Sets are designed to eliminate duplicates and provide fast lookups. + Cons: Requires creating a new set from the array elements (as shown in the script preparation code), which can add overhead. Other considerations: * **Array.prototype.indexOf()` is generally slower than `Set.delete()` because it has to iterate through the entire array to find the index, whereas sets use a hash table for fast lookups. * Using a `Set` data structure can be beneficial when you need to eliminate duplicates or perform fast membership testing. **Library and purpose** The `Array.prototype.indexOf()` method is a built-in JavaScript method that returns the index of the first occurrence of a specified value in an array. The `Array.prototype.splice()` method removes elements from an array at a specified position. **Special JS feature or syntax (none)** There are no special features or syntax being tested in this benchmark. **Other alternatives** Some alternative approaches to using `Set.delete()` include: * **Using Array.prototype.filter()**: Instead of removing individual elements, you could use `filter()` to create a new array with only the desired elements. This approach can be slower for large datasets. * **Using a custom data structure**: Depending on your specific use case, you might consider implementing a custom data structure that combines the benefits of arrays and sets. It's worth noting that MeasureThat.net is designed to provide an objective comparison of different JavaScript implementations and libraries. The benchmark framework allows you to create custom benchmarks like this one to compare the performance of different approaches.
Related benchmarks:
Splice vs shift to remove at beginning of array (fixed from slice)
Array splice vs delete
Splice vs Shift to remove from the beginning
set delete vs array splice
Array.splice(0, N) vs Array.length === N
Comments
Confirm delete:
Do you really want to delete benchmark?