Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice, splice (new)
(version: 0)
Comparing performance of:
slice vs splice
Created:
one year 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(0, 500), 9999, ...list.slice(500)];
splice
const newList = list; newList.splice(500, 0, 9999); list = newList;
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 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0
Browser/OS:
Firefox 125 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
85.4 Ops/sec
splice
1491.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the JavaScript microbenchmark you've provided. **Benchmark Overview** The benchmark measures the performance of two different approaches for inserting an element into an array in JavaScript: 1. Using `Array.prototype.slice()` 2. Using `Array.prototype.splice()` with a new array created from slicing and concatenating **Options Compared** In this benchmark, we have two options being compared: * Option 1: **Slice Method** (`slice`) * Option 2: **Splice Method** (`splice`) The slice method involves creating a new array by taking a subset of the original array using `Array.prototype.slice()`, and then inserting the element at the desired position. The splice method, on the other hand, modifies the original array in-place without creating a new one. **Pros and Cons** **Slice Method (Option 1)** Pros: * Can be faster because it avoids the overhead of modifying an array in-place. * May have better cache locality due to the contiguous nature of sliced arrays. Cons: * Creates a new array, which can lead to increased memory allocation and garbage collection. * May not be as efficient for large arrays due to the overhead of creating a new array. **Splice Method (Option 2)** Pros: * Modifies the original array in-place, avoiding the need for extra memory allocations. * Can be more efficient for large arrays because it only modifies the existing array. Cons: * May have slower performance due to the overhead of modifying an array in-place. * Can lead to poor cache locality due to the random access nature of splice operations. **Library and Special JS Feature** In this benchmark, none of the test cases use any external libraries or special JavaScript features. The focus is solely on comparing the performance of the two methods. **Other Alternatives** If you're interested in exploring other alternatives, here are a few options: * Using `Array.prototype.slice()` with the `reverse()` method to insert an element at the beginning of the array. * Using `Array.prototype.concat()` to concatenate two arrays and then inserting the element into the resulting array. * Using `Array.prototype.push()` or `Array.prototype.unshift()` to append or prepend elements to the end or beginning of the array, respectively. Keep in mind that these alternatives may not provide a direct comparison to the slice and splice methods, but can be interesting variations on the theme.
Related benchmarks:
Subarray - Splice vs Slice
Slice v splice
truncating array: slice vs splice
slice, splice
Comments
Confirm delete:
Do you really want to delete benchmark?