Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
splice vs delete on different types random index
(version: 0)
Comparing performance of:
deleteBySplice vs deleteByDelete
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script> const numArr = new Array(1e5).fill(1); const stringArr = new Array(1e5).fill('m1c6kzws0iubt8g0zlsug14i'); const arrArr = new Array(1e5).fill([1,2,3]); const objArr = new Array(1e5).fill({a: 1}); var array = [].concat(numArr, stringArr, objArr, arrArr); var index1 = Math.floor(Math.random() * 1e5) var index2 = Math.floor(Math.random() * 1e5) var index3 = Math.floor(Math.random() * 1e5) </script>
Script Preparation code:
function deleteBySplice (array, index) { if (index !== -1) { array.splice( index, 1 ); } } function deleteByDelete (array, index) { if (index !== -1) { delete array[index]; } }
Tests:
deleteBySplice
deleteBySplice( array, index1 ); deleteBySplice( array, index2 ); deleteBySplice( array, index3 );
deleteByDelete
deleteByDelete( array, index1 ); deleteByDelete( array, index2 ); deleteByDelete( array, index3 );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
deleteBySplice
deleteByDelete
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
deleteBySplice
1367.2 Ops/sec
deleteByDelete
1865224.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is tested, compared, and other considerations. **Benchmark Definition** The benchmark tests two approaches to delete elements from an array: `deleteBySplice` and `deleteByDelete`. Both methods are used on different types of arrays (numeric, string, nested array, and object). **Options Compared** * `deleteBySplice`: This method uses the `splice()` method to remove elements from the array. It takes two parameters: the index and the number of elements to remove. * `deleteByDelete`: This method uses the `delete` operator to delete properties (i.e., elements) directly from the object. **Pros and Cons** * `deleteBySplice`: + Pros: Generally faster and more efficient, as it modifies the array in place. + Cons: Can be slower for large arrays due to the overhead of the `splice()` method. * `deleteByDelete`: + Pros: Can be faster for very large arrays or when dealing with objects that are already sparse (i.e., has few properties). + Cons: Slower for small arrays, as it creates a new object and then assigns it to the original array. **Library** There is no explicit library mentioned in the benchmark definition. However, `splice()` is a built-in JavaScript method, while `delete` is also a native operator. **Special JS Feature/Syntax** None are explicitly used in this benchmark. **Other Considerations** * The benchmark uses a mix of array types (numeric, string, nested array, and object) to test the performance of both methods. * The use of `Math.floor(Math.random() * 1e5)` generates random indices for deletion, which helps to distribute the workload evenly across different elements in the arrays. * The benchmark repeats the deletion operation three times for each method, which increases the chance of catching any performance differences. **Alternatives** Other alternatives for deleting elements from an array could include: * Using `filter()` instead of `splice()` or `delete`. * Using a library like Lodash that provides a more efficient implementation of array manipulation functions. * Using a data structure like a Set, which can provide faster lookup and deletion times. Keep in mind that the choice of method depends on the specific use case and performance requirements.
Related benchmarks:
splice vs delete on different types
Ryan Test Benchmark
remove by splice vs filter array v5
Remove by splice vs spread
Comments
Confirm delete:
Do you really want to delete benchmark?