Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
DataView vs Uint8Array by bytes
(version: 0)
Comparing performance of:
DataView vs Uint8Array by byte vs Uint8Array with set
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var size = 0xFF; var buf = new ArrayBuffer(size); var view = new DataView(buf); var byte = new Uint8Array(buf); var data = []; for (let i = 0; i < size; i++) { data[i] = 100*Math.random(); }
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, 0);
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:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
DataView
2021099264.0 Ops/sec
Uint8Array by byte
2058639488.0 Ops/sec
Uint8Array with set
1076342.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare three approaches for setting values in a Uint8Array: 1. **DataView**: Using the DataView API to set values directly on the ArrayBuffer. 2. **Uint8Array by byte**: Setting individual bytes of the Uint8Array using indexing (e.g., `byte[i] = data[i]`). 3. **Uint8Array with set**: Using the `set()` method of the Uint8Array, which sets all elements from an array to the Uint8Array. **Comparison** The benchmark aims to measure the performance difference between these three approaches. The test case uses a fixed-size ArrayBuffer (0xFF bytes) and populates it with random values. **Library and Features** No specific libraries or features are used in this benchmark, aside from standard JavaScript APIs. **Options Compared** Here's a brief overview of each approach: 1. **DataView**: This approach sets values directly on the ArrayBuffer using the DataView API. It provides a way to access and manipulate binary data in an ArrayBuffer without having to manually manage memory. * Pros: Efficient, as it avoids manual memory management and indexing. Can be faster for large datasets. * Cons: May require additional setup and understanding of the DataView API. 2. **Uint8Array by byte**: This approach sets individual bytes of the Uint8Array using indexing (e.g., `byte[i] = data[i]`). It's a straightforward, language-agnostic way to set values in a Uint8Array. * Pros: Easy to understand and implement. No additional library or API knowledge required. * Cons: May be slower due to indexing operations, which can incur overhead. 3. **Uint8Array with set**: This approach uses the `set()` method of the Uint8Array, which sets all elements from an array to the Uint8Array. * Pros: Convenient and easy to use, as it leverages a standard JavaScript API. * Cons: May be slower than other approaches due to the overhead of creating an array and copying its contents. **Other Considerations** When choosing between these approaches, consider the following factors: * Performance: If you're working with large datasets or performance-critical code, using DataView might provide better results. For smaller datasets or non-performance critical code, Uint8Array by byte or with set might be sufficient. * Complexity: DataView requires additional setup and understanding of its API, while Uint8Array by byte and with set are more straightforward to implement. **Alternatives** If you're looking for alternative approaches, consider: * **Buffer**: The Buffer class is similar to ArrayBuffer but provides a higher-level interface for working with binary data. * **Typed Arrays**: Other typed arrays like Int32Array, Float64Array, or Uint16Array can be used in place of Uint8Array, depending on your specific use case. Keep in mind that the performance differences between these approaches will vary depending on the specific scenario and browser. The benchmark results provided are just a starting point for understanding the relative performance characteristics of each approach.
Related benchmarks:
copy ArrayBuffer: DataView vs Uint8Array.set vs Float64Array.set vs by bytes
DataView vs Uint8Array by bytes vs Native Array
DataView vs Uint8Array by bytes vs Uint8Array set Uint8Array vs DataView loop
copy ArrayBuffer: DataView vs Uint8Array.set vs Float64Array.set vs by bytes (2)
Comments
Confirm delete:
Do you really want to delete benchmark?