Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set array index: slice vs Object.assign v2
(version: 0)
Comparing performance of:
splice vs Object.assign spread vs Object.assign slice vs Object.assign orig
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
splice
var index = 2; var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; var output = [ ...array.slice(0, index), 10, ...array.slice(index + 1) ];
Object.assign spread
var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; var output = Object.assign([...array], {2: 10});
Object.assign slice
var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; var output = Object.assign(array.slice(), {2: 10});
Object.assign orig
var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; var output = Object.assign([...array], {2: 10});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
splice
Object.assign spread
Object.assign slice
Object.assign orig
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):
The provided benchmark measures the performance of three different approaches to modify an array in JavaScript: 1. **Slice**: This approach uses the `slice()` method to create a new array with a specified subset of elements from the original array. The test case creates a sample array and attempts to insert a value at index 2 using both `array.slice(0, index)` (exclusive) and `array.slice(index + 1)` (exclusive). Pros: * Easy to understand and implement * Widely supported by most JavaScript engines Cons: * Creates a new array, which can lead to increased memory usage * May not be as efficient for large arrays due to the overhead of creating a new array object 2. **Object.assign**: This approach uses the `Object.assign()` method to create a new object by merging multiple source objects into one. In this test case, it is used to merge an array with another object containing a single key-value pair. Pros: * Can be more efficient than slice for large arrays, as it avoids creating a new array object * Supports spread syntax (new in ECMAScript 2015) Cons: * Requires a compatible browser or Node.js version that supports `Object.assign` * May have performance implications when dealing with very large objects 3. **Spread operator**: This approach uses the spread operator (`...`) to create a new array by spreading elements from an existing array. Pros: * Similar syntax to Object.assign for merging arrays * Supports modern browsers and Node.js versions (ECMAScript 2015 and later) Cons: * Requires a compatible browser or Node.js version that supports the spread operator In this specific benchmark, the results show that: * **Object.assign slice** performs best with an average execution rate of approximately 3.77 executions per second. * **splice** is slower, with an average execution rate of around 1.76 executions per second. * **Object.assign orig** and **Object.assign spread** have similar performance, with averages of about 1.70 and 1.69 executions per second, respectively. The library used in the benchmark is none, as it only involves native JavaScript operations.
Related benchmarks:
Slice vs splice
Slice vs splice forked
slice vs get by index
set array index: slice vs Object.assign vs slice concacct vs slice and set
copy array: slice vs Object.assign
Comments
Confirm delete:
Do you really want to delete benchmark?