Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice VS splice
(version: 0)
100k list splice and shift win, they mutate list slice loose, it creates a copy of list 7.5x slower
Comparing performance of:
slice vs splice
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var list = []; for (var i = 0; i < 1000 * 1000; i++) { list.push(i); }
Tests:
slice
list = list.slice(500);
splice
list.filter(function(item){return item !== 500});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
slice
splice
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 test case for you. **What is being tested?** The benchmark measures the performance of two approaches to remove an item from an array in JavaScript: 1. **`slice()`**: Returns a shallow copy of a portion of an array, leaving the original array unchanged. 2. **`splice()`**: Removes an element from an array and shifts all subsequent elements down by one position. **Options compared** The benchmark compares the performance of these two approaches when used to remove an item from a large list (100,000 elements). **Pros and Cons of each approach:** 1. **`slice()`**: * Pros: Creates a new array with the desired elements, leaving the original array unchanged. This can be beneficial if you need to preserve the original data. * Cons: Can lead to unnecessary memory allocation and copying of large arrays, which can negatively impact performance. 2. **`splice()`**: * Pros: Mutates the original array in place, reducing memory allocation and copying. * Cons: Modifies the original array, which might not be desirable if you need to preserve the data. **Library and purpose** The `filter()` function is used in the second test case. It creates a new array with all elements that pass the test implemented by the provided function. In this case, it's used to remove an item (500) from the list. No special JavaScript features or syntax are used in these test cases. **Other alternatives** If you don't want to use `slice()` or `splice()`, you can also consider using: 1. **`findIndex()`**: Returns the index of the first element that satisfies the provided testing function. You can then use this index to remove the item from the array. 2. **`indexOf()`** and **`splice()` combination**: Use `indexOf()` to find the index of the item, then use `splice()` to remove it. However, these alternatives might not be as efficient as using `slice()` or `splice()`, especially for large arrays. In summary, the benchmark tests the performance of two common approaches to removing an item from an array in JavaScript: `slice()` (creating a new array with the desired elements) and `splice()` (modifying the original array in place).
Related benchmarks:
slice VS splice VS shift: who is the fastest to keep constant size (fork no string push)
slice VS splice VS shift: who is the fastest to keep constant size 100
slice VS splice : who get first 10 items faster
slice VS splice VS shift: smaller list, copy last half of array
slice VS splice VS shift: who is the fastest to keep constant size [VARIANT]
Comments
Confirm delete:
Do you really want to delete benchmark?