Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
copy ArrayBuffer: DataView vs Uint8Array.set vs Float64Array.set vs by bytes vs function
(version: 0)
Comparing performance of:
DataView vs Uint8Array by byte vs Uint8Array with set vs with Float64Array vs function copy
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(); } function copy(dst, src, size) { for (let i=0;i<size;i++)dst[i]=src[i]; }
Tests:
DataView
for (let i; i < size; i++) { view.setUint8(i, data[i]); }
Uint8Array by byte
for (let i; i < size; i++) { byte[i] = data[i]; }
Uint8Array with set
byte.set(data);
with Float64Array
(new Float64Array(buf, 0, size/8)) .set(new Float64Array(data.buffer, 0, size/8));
function copy
copy(byte, data, data.length)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
DataView
Uint8Array by byte
Uint8Array with set
with Float64Array
function copy
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):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** The benchmark is designed to compare different approaches for copying data from an ArrayBuffer into a Uint8Array. The test case uses a JavaScript ArrayBuffer, which is a mutable, typed view of a binary buffer in memory. **Test Cases** There are four main test cases: 1. **DataView**: This test case uses the DataView API to set each byte of the Uint8Array. 2. **Uint8Array by byte**: This test case simply assigns each byte of the Uint8Array using the array indexing syntax. 3. **Uint8Array with set**: This test case uses the `set()` method provided by the Uint8Array class to set its elements. 4. **with Float64Array**: This test case creates a new Float64Array and sets its elements from the ArrayBuffer using another Float64Array as an argument. **Library: DataView** The DataView API is used in the first two test cases (DataView and Uint8Array by byte). The DataView API provides a way to view a binary buffer as a sequence of numbers, where each number has at most four bytes. In this case, it's used to set each byte of the Uint8Array. **Pros and Cons** * **DataView**: Pros: allows for efficient and direct access to individual bytes in the ArrayBuffer. Cons: may be slower than other methods due to its overhead. * **Uint8Array by byte**: Pros: simple and straightforward approach. Cons: requires manual indexing, which can lead to errors if not done correctly. **Uint8Array with set** The `set()` method provided by the Uint8Array class is used in this test case. This method is a more modern and efficient way to set the elements of an array. Pros: provides a simpler and more readable syntax than manual indexing. Cons: may be slower than other methods due to its overhead. **Float64Array** The Float64Array class is created in this test case, but it's not used to copy data from the ArrayBuffer. Instead, another Float64Array is passed as an argument to set its elements. Pros: provides a convenient way to create a new array and set its elements. Cons: may be slower than other methods due to its overhead. **Function copy** This test case uses a custom `copy()` function to assign each byte of the Uint8Array. This approach is often used for performance-critical code, but it can also lead to errors if not implemented correctly. Pros: provides fine-grained control over the copying process. Cons: requires manual indexing and error handling. **Other Alternatives** There are other ways to copy data from an ArrayBuffer into a Uint8Array, such as using the `subarray()` method or creating a new array using the spread operator (`[...])`. However, these methods may not be as efficient or readable as the ones used in the benchmark. In summary, this benchmark compares different approaches for copying data from an ArrayBuffer into a Uint8Array. The test cases demonstrate the performance differences between using DataView, manual indexing, and other methods like `set()` or a custom function.
Related benchmarks:
copy ArrayBuffer: DataView vs Uint8Array.set vs Float64Array.set vs by bytes
DataView vs Uint8Array by bytes vs Native Array
copy ArrayBuffer: DataView vs Uint8Array.set vs Float64Array.set vs by bytes v2
copy ArrayBuffer: DataView vs Uint8Array.set vs Float64Array.set vs by bytes (2)
Comments
Confirm delete:
Do you really want to delete benchmark?