Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
copy ArrayBuffer: DataView vs Uint8Array.set vs by bytes ag-sockets v2
(version: 0)
Comparing performance of:
DataView vs Uint8Array by byte vs Uint8Array with set
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var size = 0xFFFF; var buf = new ArrayBuffer(size); var view = new DataView(buf); var byte = new Uint8Array(buf); var data = new Uint8Array(size); for (let i = 0; i < size; i++) { data[i] = 100*Math.random(); } window._data = data; window._buf = buf;
Tests:
DataView
data[Date.now()%data.length] = 43;data[Date.now()%data.length] = 65; for (let i; i < size; i++) { view.setUint8(i, data[i]); }
Uint8Array by byte
data[Date.now()%data.length] = 43;data[Date.now()%data.length] = 65; for (let i; i < size; i++) { byte[i] = data[i]; }
Uint8Array with set
data[Date.now()%data.length] = 43;data[Date.now()%data.length] = 65; byte.set(new Uint8Array(data.buffer, data.byteOffset, data.length));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
DataView
Uint8Array by byte
Uint8Array with set
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark that measures the performance of three approaches for copying data from an ArrayBuffer to a Uint8Array: 1. Using `DataView.set()` 2. Setting each byte individually using `Uint8Array[i] = data[i]` 3. Using `Uint8Array.set()` with a subarray **Options Compared** The benchmark compares the performance of these three approaches: * `DataView.set()`: This method sets the value at a specific index in a DataView. * Setting each byte individually using `Uint8Array[i] = data[i]`: This approach involves accessing each element of the Uint8Array individually to set its value. * Using `Uint8Array.set()` with a subarray: This method sets the values in a specified range of an Uint8Array. **Pros and Cons of Each Approach** ### 1. `DataView.set()` * **Pros**: * Fast, as it involves only a single memory access. * Efficient use of CPU resources, since the browser's interpreter can optimize this operation. * **Cons**: * Limited support for certain data types (e.g., big-endian integers). * May not be suitable for large datasets due to its reliance on a DataView. ### 2. Setting each byte individually * **Pros**: * Can handle any type of data, regardless of the endianness. * Suitable for large datasets, since it only requires iterating over the elements of the Uint8Array. * **Cons**: * Slower than `DataView.set()` due to multiple memory accesses per element. * Inefficient use of CPU resources. ### 3. Using `Uint8Array.set()` * **Pros**: * Similar performance to `DataView.set()`, as it also only requires a single memory access per byte. * Handles large datasets efficiently, since it uses an optimized C++ implementation for the `set()` method. * **Cons**: * Limited support for certain data types (e.g., big-endian integers). * May not be suitable for smaller datasets due to its reliance on a subarray. **Library Usage** None of the provided benchmark test cases use any external libraries. However, some browsers' implementations might rely on internal libraries or native code optimizations for these methods. **Special JS Feature/Syntax** The benchmark utilizes a feature specific to JavaScript: **Closures** (not explicitly mentioned in the JSON but implicit in `window._data` and `window._buf`). Closures allow the variable `_data` to retain its value even after the script finishes execution, making it accessible across multiple lines of code. **Other Alternatives** If you're interested in exploring other alternatives for copying data from an ArrayBuffer to a Uint8Array, consider these methods: * Using `TypedArrays` (e.g., `Float64Array`, `Int32Array`) instead of Uint8Array. * Using ` DataView.set()` with different types (e.g., `BigInt64Array`) or endianness (big-endian). * Implementing a custom loop for copying data from the ArrayBuffer to the Uint8Array. When exploring alternative approaches, keep in mind factors such as performance, compatibility, and potential limitations of each method.
Related benchmarks:
copy ArrayBuffer: DataView vs Uint8Array.set vs Float64Array.set vs by bytes
copy ArrayBuffer: DataView vs Uint8Array.set vs by bytes ag-sockets
copy ArrayBuffer: DataView vs Uint8Array.set vs Float64Array.set vs by bytes vs function
copy ArrayBuffer: DataView vs Uint8Array.set vs Float64Array.set vs by bytes (2)
Comments
Confirm delete:
Do you really want to delete benchmark?