Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
typed array copyWithin vs set
(version: 0)
comparing speed of copyWithin vs set
Comparing performance of:
set vs copyWithin
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array1_1e6 = Float32Array.from({length: 1e6}, Math.random); var target = 1e5; var start = 2e5; var end = 3e5; var array1_1e5 = Float32Array.from({length: 1e5}, Math.random); var array2_1e5 = Float32Array.from({length: 1e5}, Math.random);
Tests:
set
array1_1e5.set(array2_1e5);
copyWithin
array1_1e6.copyWithin(target, start, end);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
set
copyWithin
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
set
77931.8 Ops/sec
copyWithin
76553.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net! **What is being tested?** The provided JSON benchmark compares the performance of two approaches: `set` and `copyWithin` methods for setting elements in typed arrays. **Options compared:** 1. **`array1_1e5.set(array2_1e5)`**: This method sets all elements in `array1_1e5` to the values in `array2_1e5`. 2. **`array1_1e6.copyWithin(target, start, end)`**: This method copies elements from `array2_1e6` to `array1_1e6` starting at index `target` and ending at `end`. **Pros and Cons of each approach:** **`set`:** Pros: * Easier to read and understand * Can be more efficient for small arrays Cons: * Requires creating a new typed array, which can lead to slower performance for large arrays due to memory allocation **`copyWithin`:** Pros: * Avoids creating a new typed array, reducing memory allocation overhead * Can be faster for larger arrays since it only copies elements, not allocates new space Cons: * May have additional overhead due to indexing and bounds checking **Library usage:** The benchmark uses the `Float32Array` constructor from the JavaScript built-in types. This is a typed array that stores 32-bit floating-point numbers. **Special JS features or syntax:** None explicitly mentioned, but it's worth noting that both methods use arrays as input and output, which is a common pattern in JavaScript. **Benchmark preparation code explanation:** The script prepares two large typed arrays (`array1_1e5` and `array2_1e5`) using the `Float32Array.from()` method. It then defines some constants for indexing and execution timing (start and end times). **Individual test cases:** Each test case represents a separate benchmark: * **"set"`**: This benchmark calls `set()` on `array1_1e5` with `array2_1e5` as its argument. * **"copyWithin"`**: This benchmark calls `copyWithin()` on `array1_1e6` with indices `target`, `start`, and `end`. **Latest benchmark result:** The latest results show that the `set()` method performed slightly faster than `copyWithin()` in this specific test case. However, note that these results may vary depending on the system configuration and browser version. Other alternatives for testing typed array operations might include: * Using other types of arrays (e.g., `Int32Array`, `Uint8Array`) * Adding more test cases with varying input sizes or complexity * Comparing performance against other libraries or frameworks that implement similar functionality
Related benchmarks:
JavaScript array copy methods for() vs spread operator
Lodash cloneDeep vs. Lodash clone vs. Array.slice() vs. Array.slice(0) vs. Object.assign()
Array shallow copy - slice(0) vs conditional for() loop
Slice vs spread array
Array.slice() vs. Spread operator (10000 items)
Comments
Confirm delete:
Do you really want to delete benchmark?