Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Fastest Array Shallow Copy21312312
(version: 1)
Testing the results for the fastest approach of shallow copying an array in JavaScript (concat, slice, for loop, and while loop).
Comparing performance of:
Array.concat vs Array.slice
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
Array.concat
var a = [ "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello" ]; var other = Array.from(a);
Array.slice
var a = [ "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello" ]; var b = a.slice();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.concat
Array.slice
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.concat
63243080.0 Ops/sec
Array.slice
105881800.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON tests the performance of various methods for shallow copying an array in JavaScript. Specifically, it compares two approaches: using the `Array.from` method and the `Array.slice` method. Each of these methods has its own characteristics, advantages, and potential drawbacks. ### Test Cases 1. **Array.concat** - **Benchmark Definition**: ```javascript var a = [ /* array of "hello" strings */ ]; var other = Array.from(a); ``` - **Test Name**: "Array.concat" 2. **Array.slice** - **Benchmark Definition**: ```javascript var a = [ /* array of "hello" strings */ ]; var b = a.slice(); ``` - **Test Name**: "Array.slice" ### Pros and Cons of Each Approach - **Array.from** - **Pros**: - Clear syntax that indicates copying an iterable (including arrays). - Can accept a mapping function as a second argument to transform the elements while copying. - **Cons**: - This approach might be less performant than `slice` in certain cases, particularly for large arrays, due to the overhead of the function call and additional features. - **Array.slice** - **Pros**: - Simple and straightforward to use, designed specifically for array copying. - Generally faster than `Array.from` for copying a regular array, as shown in the benchmark results. - **Cons**: - Less flexible in terms of transformation, as it simply creates a shallow copy without any ability to apply a mapping function directly. ### Benchmark Results From the latest benchmark results, we see the following performance indicators: - **Array.slice** - **Executions Per Second**: 105,881,800.0 - **Browser**: Chrome 131 - **Operating System**: Mac OS X 10.15.7 - **Array.concat** - **Executions Per Second**: 63,243,080.0 - **Browser**: Chrome 131 - **Operating System**: Mac OS X 10.15.7 The results clearly indicate that `Array.slice` has a much higher execution rate than `Array.from` in this particular test scenario, thus suggesting that it may be a better choice for performance-critical applications where shallow copying of arrays is frequent. ### Other Alternatives for Shallow Copying Arrays While the benchmark focuses on `Array.from` and `Array.slice`, there are alternative methods to perform shallow copies in JavaScript, including: - **Spread Operator ([...])**: ```javascript var b = [...a]; ``` - **Pros**: - Very readable and concise syntax. - Can be used in various contexts, such as array concatenation. - **Cons**: - It may have slightly different performance characteristics compared to `slice`, but typically this method is also fast. - **Array.prototype.concat()**: ```javascript var b = a.concat(); ``` - **Pros**: - Returns a new array with the combined contents, making it useful for copying while also combining arrays. - **Cons**: - Can be less intuitive for simple copying, as it is often used for merging. ### Conclusion In summary, this benchmark evaluates two methods for shallow copying arrays in JavaScript: `Array.from` and `Array.slice`. The results indicate that `Array.slice` provides superior performance in this scenario. While both methods are valid approaches, developers should consider the specific use case and performance implications when choosing how to copy arrays, as well as potential alternatives like the spread operator and `concat` method.
Related benchmarks:
Array.prototype.concat vs splice for joining two arrays1
Array.prototype.concat vs splice for joining two arrays2
array insertAt with slice+concat VS splice
Array.prototype.concat vs splice vs push for joining two arrays
JS Fastest Array Shallow Copy
JavaScript Array Copy
JavaScript Fastest Array Copy
JavaScript Array Copy Test 2
JavaScript Array Copy 2
Comments
Confirm delete:
Do you really want to delete benchmark?