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 123123
(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:
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(1);
splice
list.push('splice'); list.splice(0, 1);
shift
list.push('shift', 'pop'); list.shift(); list.pop();
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:
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):
Let's break down what's being tested in the provided benchmark. **Benchmark Definition JSON** The test aims to compare three different methods for removing elements from an array while maintaining its original size: 1. `splice`: Replaces the element at the specified position with another value (or removes the element if no replacement is given). 2. `shift`: Removes the first element of the array and returns it. 3. `slice`: Creates a new array containing all elements except those in the specified range. The test consists of 100,000 elements in an array, which will be used as the basis for the comparisons. **Options Compared** * `splice` vs `shift` vs `slice` + **Pros and Cons:** - `splice`: Can remove multiple elements at once, but it's slower due to creating a new array. - `shift`: Efficiently removes one element from the start of the array, but it returns the removed element, which might not be desirable in all cases. - `slice`: Creates a new array without modifying the original one, making it less efficient. * **Library Considerations:** None * **Special JS Features/Syntax:** None mentioned **Benchmark Results** The latest results show that: 1. `splice` is the fastest method (3424.5654296875 executions per second). 2. `shift` is slower than `splice`, but faster than `slice` (1197.7412109375 executions per second and 114.33365631103516 executions per second, respectively). **Other Alternatives** If you were to consider alternative methods for removing elements from an array while maintaining its original size: * `concat` with negative indexing: This method creates a new array by concatenating two arrays, one of which contains the index positions to be skipped. While it might seem like a good option, it's actually slower than using `slice`. * Using `Array.prototype.map()` or other methods that create a new array: These methods are generally slower and more memory-intensive than using `splice`, `shift`, or `slice`. Keep in mind that the choice of method depends on your specific use case and performance requirements.
Related benchmarks:
slice VS splice: who is the fastest to keep constant size
slice VS splice (clone) VS shift: who is the fastest to keep constant size
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?