Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Dataview vs Uint8Array - read byte
(version: 0)
Comparing performance of:
DataView vs Uint8Array
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var size = 1024; var buffer = new ArrayBuffer(size); var bytes = new Uint8Array(buffer); var dv = new DataView(buffer); for (var i = 0; i < size; i++) dv.setUint8(i, Math.ceil(Math.random() * 255));
Tests:
DataView
for (var i = 0; i < size; i++) dv.getUint8(i);
Uint8Array
for (var i = 0; i < size; i++) bytes[i];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
DataView
Uint8Array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
DataView
278788.1 Ops/sec
Uint8Array
323897.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's break down the provided JSON data and benchmark preparation code. **Benchmark Definition** The benchmark compares two approaches to read a single byte from a buffer: 1. Using `DataView` (an object that allows for direct manipulation of an ArrayBuffer) 2. Using `Uint8Array` (a typed array that can be used to represent an array of unsigned 8-bit integers) **Test Case 1: DataView** The test case uses the following code: ```javascript for (var i = 0; i < size; i++) dv.getUint8(i); ``` Here, `dv` is a `DataView` object created from an ArrayBuffer. The test case simply calls the `getUint8()` method on the `dv` object to read a single byte at each index. **Test Case 2: Uint8Array** The test case uses the following code: ```javascript for (var i = 0; i < size; i++) bytes[i]; ``` Here, `bytes` is a `Uint8Array` object created from an ArrayBuffer. The test case simply accesses each element of the array using its index. **Pros and Cons** * **DataView** + Pros: Direct access to the underlying buffer, can read/write single bytes or larger data types. + Cons: May have performance overhead due to the need to create a `DataView` object and perform bounds checking. * **Uint8Array** + Pros: A typed array that provides a simple way to access elements using indices. Can be used for large-scale numerical computations. + Cons: May incur performance overhead when accessing individual bytes, as it needs to perform bounds checking. **Other Considerations** * The test uses an ArrayBuffer of size 1024 and populates it with random values using `Math.ceil(Math.random() * 255)`. This simulates a real-world scenario where data is being read from or written to memory. * The test runs for each approach, measuring the number of executions per second (EPS). The EPS value indicates how many times the respective code can execute in one second. **Alternative Approaches** Other alternatives for reading single bytes from a buffer include: 1. Using `ArrayBuffer.prototype.slice()` to create a new ArrayBuffer view and access the byte at each index. 2. Using a library like `TypedArrays` or `arraybuffer- viewer` that provides optimized methods for working with ArrayBuffers and typed arrays. However, these alternatives may incur additional overhead or require more complex code, so they might not be suitable for performance-critical applications. **Special JS Feature/Syntax** None are used in this benchmark. The code uses standard JavaScript features like loops, conditionals, and array access. If you have any further questions or would like me to clarify anything, feel free to ask!
Related benchmarks:
Dataview vs Custom - Read uint8
Dataview vs Uint8Array - read byted
copy ArrayBuffer: DataView vs Uint8Array.set vs Float64Array.set vs by bytes
copy ArrayBuffer: DataView vs Uint8Array.set vs Float64Array.set vs by bytes (2)
Comments
Confirm delete:
Do you really want to delete benchmark?