Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spread- concat-unshift-twa-arrays
(version: 0)
spread- concat-unshift-twa-arrays
Comparing performance of:
arrayUnshift vs arrayConcat vs arraySpread
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const array = [1, 2, 3]; const array2 = [4, 5];
Tests:
arrayUnshift
const array = [1, 2, 3]; const array2 = [4, 5]; array.unshift(...array2);
arrayConcat
let array = [1, 2, 3]; const array2 = [4, 5]; array = array.concat(array2)
arraySpread
let array = [1, 2, 3]; const array2 = [4, 5]; array = [...array, ...array2]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
arrayUnshift
arrayConcat
arraySpread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
arrayUnshift
22699142.0 Ops/sec
arrayConcat
16306900.0 Ops/sec
arraySpread
43239244.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of JavaScript operations can be complex, but I'll break it down for you. **Benchmark Definition:** The benchmark definition is a JSON object that contains information about the test case. In this case, there are three test cases: * `spread- concat-unshift-twa-arrays`: This is the name of the benchmark, which describes what's being tested. * `Script Preparation Code` and `Html Preparation Code`: These fields contain JavaScript code that needs to be executed before running the test case. In this case, both are empty, indicating a simple benchmark. **Individual Test Cases:** Each test case represents a specific operation on arrays: 1. **arrayUnshift**: This test case checks how fast `unshift()` can modify an array by spreading another array into it. * The JavaScript code `const array = [1, 2, 3]; const array2 = [4, 5]; array.unshift(...array2);` creates two arrays and then uses the spread operator (`...`) to add all elements from `array2` to `array`, followed by a direct call to `unshift()`. * The purpose of this test is likely to compare the performance of different ways to modify an array, such as using `push()` or `unshift()`. 2. **arrayConcat**: This test case checks how fast `concat()` can modify an array by spreading another array into it. * The JavaScript code `let array = [1, 2, 3]; const array2 = [4, 5]; array = array.concat(array2)` creates two arrays and then uses the spread operator (`...`) to add all elements from `array2` to `array`, followed by a direct call to `concat()`. * Similar to `arrayUnshift`, this test is likely comparing different ways to modify an array. 3. **arraySpread**: This test case checks how fast using the spread operator (`...`) can create a new array by spreading elements from another array. **Library:** None of these test cases seem to use any specific library, but `concat()` and `unshift()` are built-in methods in JavaScript. However, if you're using older browsers or environments that don't support modern JavaScript features, some libraries might be used as workarounds (e.g., polyfills). **Special JS Features:** None of these test cases use any special JavaScript features, such as async/await, generators, or classes. **Pros and Cons:** * **Using `unshift()` vs. spread operator (`...`)**: + Using `unshift()` can be faster because it modifies the array in-place without creating a new one. + However, using the spread operator (`...`) is generally more readable and flexible when working with arrays. * **Using `concat()` vs. spread operator (`...`)**: + Like with `unshift()`, using `concat()` can be faster because it modifies the array in-place without creating a new one. + However, using the spread operator (`...`) is generally more concise and readable when working with arrays. **Other Alternatives:** * Instead of using `push()` to add elements to an array, you could use `unshift()` or the spread operator (`...`). * If you want to create a new array without modifying the original one, you could use the spread operator (`...`) or the `slice()` method. * For larger arrays, using `Array.prototype.slice()` can be more efficient than creating a new array with the spread operator (`...`). In general, when working with arrays in JavaScript, it's essential to consider performance and readability. The choice of operation depends on the specific use case and your personal preference.
Related benchmarks:
Array Spread vs concat
spread vs concat vs unshift for arrays
simple spread vs concat benchmark
JavaScript spread operator vs Array.concat performance
Comments
Confirm delete:
Do you really want to delete benchmark?