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
(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 vs shift
Created:
7 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(1);
splice
list.push('splice'); list.splice(0, 1);
shift
list.push('splice'); list.shift();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
slice
splice
shift
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 YaBrowser/24.10.0.0 Safari/537.36
Browser/OS:
Yandex Browser 24 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
101.5 Ops/sec
splice
1458.9 Ops/sec
shift
472.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net! **Benchmark Definition** The benchmark definition represents a JavaScript code snippet that is used to test the performance of different approaches. In this case, we have three benchmark definitions: 1. `list.push('slice');\nlist = list.slice(1);` (Slice) 2. `list.push('splice');\nlist.splice(0, 1);` (Splice) 3. `list.push('splice');\nlist.shift();` (Shift) Each benchmark definition is a JavaScript code snippet that pushes an element to the end of the array `list`, and then either uses `slice()` or `splice()`/`shift()` to modify the array. **Options Compared** The options being compared are: * **Slice**: The `slice()` method creates a shallow copy of a portion of an array. + Pros: Creates a new array, doesn't mutate the original array. + Cons: Can be slower due to the creation of a new array. * **Splice**: The `splice()` method modifies the array by removing or replacing elements. + Pros: Modifies the original array, can be faster than `slice()`. + Cons: Mutates the original array, can have unexpected side effects. * **Shift**: The `shift()` method removes and returns the first element of an array. + Pros: Modifies the original array, can be fast due to the removal of an element at the beginning. + Cons: Only works on arrays with more than one element. **Library** The library used in these benchmark definitions is none. The tests use built-in JavaScript methods (`slice()`, `splice()`, and `shift()`). **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes being tested in this benchmark. The tests only rely on standard JavaScript methods and built-in types. **Other Alternatives** If you're looking for alternatives to these approaches, consider: * **Array.prototype.slice.call()**: Creates a new array from an existing one. * **Array.prototype.splice() with start and end indices**: Modifies the original array by removing or replacing elements at specific positions. * **Array.prototype.shift()** (same as above): Removes and returns the first element of an array. These alternatives can be used in similar scenarios, but their performance characteristics might differ from the ones tested here.
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 100
slice VS splice VS shift: who is the fastest to keep constant size [VARIANT]
Comments
Confirm delete:
Do you really want to delete benchmark?