Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
copyWithin: is it fast?
(version: 0)
Is copyWithin fast?
Comparing performance of:
Uint8 copyWithin vs Int8 Iteration vs Uint32 copy within vs .set
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
// starting with a plain array buffer, what is fastest? var buffer = new ArrayBuffer(1024 * 1024); var originalIntView = new Uint8Array(buffer); for (var i = 0; i < originalIntView.length; i++) { originalIntView[i] = Math.round(Math.random() * 256); }
Tests:
Uint8 copyWithin
originalIntView.copyWithin(0, 512*1024, 1024*1024)
Int8 Iteration
for (var i = 0; i < 1024 * 512; i++) { originalIntView[i] = originalIntView[i + 1024 * 512]; }
Uint32 copy within
var int32View = new Uint32Array(originalIntView.buffer, originalIntView.byteOffset, originalIntView.byteLength / 4); for (var i = 0; i < 1024 * 512 / 4; i++) { int32View[i] = int32View[i + 1024 * 512 / 4]; }
.set
var other = new Uint8Array(originalIntView.buffer, originalIntView.byteOffset + originalIntView.byteLength / 2, originalIntView.byteLength / 2) originalIntView.set(other, 0)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Uint8 copyWithin
Int8 Iteration
Uint32 copy within
.set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Uint8 copyWithin
40292.0 Ops/sec
Int8 Iteration
1515.7 Ops/sec
Uint32 copy within
3996.8 Ops/sec
.set
42909.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript benchmark test case, specifically designed to compare the performance of different approaches for copying data within an array buffer. The test is focused on three scenarios: `copyWithin`, `.set`, and iterating over the array. **Script Preparation Code** The script preparation code starts with creating a plain array buffer with a size of 1MB (1024 * 1024). A `Uint8Array` view is created from this buffer, and then populated with random values. This initial setup ensures that all test cases are starting from the same state. **Options Compared** The benchmark compares four different approaches: 1. **`copyWithin`**: A built-in JavaScript method that copies a portion of an array to another location in memory. 2. **`.set`**: A method that sets the values of an array to a new array or a view. 3. **Int8 Iteration**: Manually iterating over the array and assigning each value to the next index (similar to `copyWithin`, but without using the built-in method). 4. **Uint32 copy within**: Similar to `copyWithin`, but working with 32-bit unsigned integers (`Uint32Array`) instead of 8-bit unsigned integers (`Uint8Array`). **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **`copyWithin`**: Pros: Efficient, concise, and well-supported. Cons: May not be suitable for all use cases (e.g., when working with non-numeric data types). * `.set`: Pros: Allows setting an array to a new value or view, which can be useful in certain scenarios. Cons: May have performance overhead due to the additional step of creating a new array. * **Int8 Iteration**: Pros: Hand-coding can lead to better understanding and control over memory management. Cons: Inefficient, prone to errors, and not well-suited for large datasets. * **Uint32 copy within**: Similar pros and cons as `copyWithin`, but with an additional performance overhead due to the increased data type. **Library and Special JS Features** In this benchmark, no libraries are explicitly used. However, it's worth noting that some JavaScript environments (e.g., Node.js) may have built-in optimizations or features that affect the performance of certain methods, such as `copyWithin`. **Considerations** When interpreting these results, consider the following: * The test is designed to evaluate the relative performance of different approaches in a specific scenario. * The benchmark is likely run on a desktop platform with a modern browser (Chrome 129). * The results may not be representative for other platforms, browsers, or use cases. **Alternatives** If you're interested in exploring alternative approaches or testing scenarios, here are some suggestions: * **`subarray()`**: Another built-in JavaScript method that creates a new array from a portion of an existing array. This approach is worth considering when working with large datasets. * **Native WebAssembly support**: Modern browsers and web frameworks may have native support for WebAssembly, which can provide significant performance improvements for certain types of computations. * **Third-party libraries or frameworks**: Depending on your specific use case, you may want to explore using specialized libraries or frameworks that optimize memory management and data processing.
Related benchmarks:
Maping BooleanArray vs uInt8 Array2 vs uint8 with bitMasking _2
32bit copyWithin: is it fast?
copyWithin: is it fast in Uint32Array
Maping numeric vs f32 vs f64 with add
Comments
Confirm delete:
Do you really want to delete benchmark?