Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice VS splice VS shift: who is the fastest to keep constant size 100
(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:
3 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.push('slice'); list = list.slice(100);
splice
list.push('splice'); list.splice(0, 100);
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:
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
slice
513.4 Ops/sec
splice
6807243.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark. **Benchmark Overview** The benchmark measures the performance of two JavaScript methods: `slice` and `splice`. The goal is to determine which method is faster when removing 100 elements from an array, while keeping its original size constant. In other words, we're trying to find out whether creating a copy of the array using `slice` or modifying the original array using `splice` (or shifting elements) is more efficient. **Options Compared** There are two methods being compared: 1. **Slice (`list.slice(100)`)**: This method creates a new array that includes all elements from the original array, up to the 100th element. The rest of the elements are discarded. 2. **Splice (`list.splice(0, 100)`)**: This method modifies the original array by removing the first 100 elements. **Pros and Cons** * **Slice (`slice`)**: + Pros: Creates a new, independent array copy, which can be beneficial for preserving data integrity. + Cons: Can be slower than `splice` due to the overhead of creating a new array. * **Splice (`splice`)**: + Pros: Modifies the original array in-place, which can be faster since it doesn't require creating a new array copy. + Cons: Modifies the original data, which may not be desirable in some cases. **Library and Features** There are no libraries used in this benchmark. The only JavaScript features mentioned are `slice` and `splice`, which are built-in methods for manipulating arrays. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax used in this benchmark. It's a straightforward comparison of two basic array manipulation methods. **Alternatives** If you're looking for alternative approaches, consider the following: 1. **Array.prototype.forEach() and Array.prototype.map()**: Instead of using `slice` or `splice`, you could use `forEach()` to iterate over the array elements, skipping every 100th element. 2. **Using a custom iterator**: You could implement a custom iterator that skips every 100th element when iterating over the array. 3. **Using a different data structure**: Depending on your specific use case, using a different data structure like a linked list or a queue might be more efficient for removing elements. Keep in mind that these alternatives may have additional overhead or complexity compared to simply using `slice` or `splice`.
Related benchmarks:
slice VS splice: who is the fastest to keep constant size
slice VS splice VS shift: who is the fastest to keep constant size (fork)
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 [VARIANT]
Comments
Confirm delete:
Do you really want to delete benchmark?