Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Typed Array Clone
(version: 2)
Comparing performance of:
a.slice() vs new Float64Array(a)
Created:
one year ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
var a = new Float64Array([1.2, 3.4, 5.6, 7.8, 9.10, -1.2, -3.4, -5.6, -7.8, -9.10])
Tests:
a.slice()
var b = a.slice() a = b;
new Float64Array(a)
var c = new Float64Array(a) a = c;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
a.slice()
new Float64Array(a)
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/605.1.15 (KHTML, like Gecko) Version/17.6 Safari/605.1.15
Browser/OS:
Safari 17 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
a.slice()
26432878.0 Ops/sec
new Float64Array(a)
31957052.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark provided tests the performance of different methods for cloning a typed array in JavaScript, specifically a `Float64Array`. This benchmark evaluates the speed of two different approaches for creating a copy of the original array (`a`). ### Options Compared 1. **`a.slice()` method**: - **Benchmark Definition**: `var b = a.slice()\r\na = b;` - **Test Name**: `"a.slice()"` The `slice()` method creates a shallow copy of a portion of an array into a new array object. In this case, it creates a complete copy of the existing `Float64Array`. The `slice()` method maintains the type of the original array and the contents of the array are copied as they are. 2. **`new Float64Array(a)` constructor**: - **Benchmark Definition**: `var c = new Float64Array(a)\r\na = c;` - **Test Name**: `"new Float64Array(a)"` This method constructs a new `Float64Array` and directly initializes it with the content of the existing array `a`. This method effectively copies the contents of `a`, creating a new typed array. ### Pros and Cons of Each Approach - **`a.slice()`**: - **Pros**: - Simple and intuitive syntax for copying arrays. - Preserves the type and structure of the input array (if the input is a typed array). - **Cons**: - Slightly slower performance compared to using the constructor due to the overhead of method invocation and potential internal copying mechanisms specific to the array prototype. - **`new Float64Array(a)`**: - **Pros**: - Often faster than `slice()` based on benchmark results, as it directly constructs a new typed array from the source, potentially optimizing memory allocation and copying in one step. - Type safety is guaranteed since it explicitly creates a `Float64Array`. - **Cons**: - Slightly less intuitive for developers who may not be familiar with typed arrays and their constructors compared to the common array methods, though this is a minor concern. ### Benchmark Results From the benchmark results obtained, we see the execution times for each method on Safari 17 under the following conditions: - **`new Float64Array(a)`** achieved **31,957,052 executions per second**. - **`a.slice()`** had **26,432,878 executions per second**. This indicates that the `new Float64Array()` method outperforms the `slice()` method when it comes to cloning a typed array, specifically for the given benchmark. ### Other Considerations and Alternatives While both options are suitable for cloning typed arrays, developers may consider other alternatives depending on their specific use cases: 1. **Using `Array.from()`**: - This method can also be used to create a new populated array, although the output would not be of type `Float64Array` unless explicitly converted. 2. **Spreading Operator** (`...`): - This operator allows for easy copying of arrays, e.g., `let b = [...a]`. However, it does not create a typed array, so further conversion would be necessary if typed behavior is required. 3. **`set()` method**: - For specific cases where performance is paramount, utilizing the `set()` method of typed arrays may help if you're working within a context that avoids creating intermediate typed arrays, but this is less common for mere copying operations. Ultimately, the choice between `a.slice()` and `new Float64Array(a)` will largely depend on performance requirements and code clarity. For performance-critical applications dealing with large amounts of numerical data, leveraging typed arrays and the constructor method may lead to more efficient code.
Related benchmarks:
set array index: slice vs Object.assign
array move element
array shift - array slice
Array.slice vs manual clone
Copy to float32Array
set array index: slice vs Object.assign vs slice concacct vs slice and set
Get last element of array2
js array copy any type
slice vs subarray vs set - million elements
Comments
Confirm delete:
Do you really want to delete benchmark?