Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Binary to boolean with DataView: readUint8() with for loop vs. slice() with map()
(version: 0)
Comparing performance of:
readUint8 with for loop vs Slice with map
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
readUint8 with for loop
const length = 1000; const arrayBuffer = new ArrayBuffer(length); const array = new Uint8Array(arrayBuffer); for (let i = 0; i < length; i++) { array[i] = 22; } const dataView = new DataView(arrayBuffer); const resultArray = []; for (let i = 0; i < length; i++) { const value = dataView.getUint8(i); resultArray.push(Boolean(value)); }
Slice with map
const length = 1000; const arrayBuffer = new ArrayBuffer(length); const array = new Uint8Array(arrayBuffer); for (let i = 0; i < length; i++) { array[i] = 22; } const dataView = new DataView(arrayBuffer); const transferArray = new Uint8Array(dataView); const resultArray = [...transferArray].map(value => Boolean(value));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
readUint8 with for loop
Slice with map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
readUint8 with for loop
7269.0 Ops/sec
Slice with map
436898.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its options. **What is being tested?** The benchmark compares two approaches to convert binary data from a `DataView` object to an array of booleans: 1. **readUint8 with for loop**: This approach uses the `getUint8()` method of the `DataView` object to read individual bytes from the buffer and then pushes the result of `Boolean(value)` onto an array using a for loop. 2. **Slice with map**: This approach uses the `slice()` method of the `Uint8Array` object to extract a subset of bytes from the buffer, followed by applying the `map()` function to convert each byte to a boolean value. **Options comparison** The two approaches differ in their usage of native array methods: * **readUint8 with for loop**: Uses a traditional for loop to iterate over the buffer, which can be less efficient than using modern array methods. * **Slice with map**: Utilizes `slice()` and `map()`, which are more concise but may have performance overhead due to additional function calls. **Pros and cons** **readUint8 with for loop:** Pros: * Easy to understand and implement * No potential performance overhead from additional function calls Cons: * Less efficient than using modern array methods * More verbose code **Slice with map:** Pros: * More concise and readable code * Potential performance benefits due to vectorization (automatic parallel processing) Cons: * May have additional performance overhead due to extra function calls * Requires knowledge of array methods and their potential pitfalls **Library usage** The benchmark uses the following libraries/libraries-specific features: * `DataView`: a native JavaScript object for working with binary data. * `Uint8Array` and `ArrayBuffer`: native JavaScript objects for working with arrays and buffers. No external libraries are required. However, if you were to use alternative approaches that might involve external libraries (e.g., using Python or C++), you would need to consider the additional dependencies and performance implications. **Special JS features** There is no special JS feature or syntax being used in this benchmark beyond what's native to JavaScript.
Related benchmarks:
uint8array vs dataview extract
Binary to boolean with DataView: readUint8() with for loop vs. slice() with map() vs. slice() with Array.from()
Binary to boolean with DataView: slice() with map() vs. slice() with for loop
uint32 DataView vs Direct Calculate
Comments
Confirm delete:
Do you really want to delete benchmark?