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() vs. slice() with Array.from()
(version: 0)
Comparing performance of:
readUint8() with for loop vs slice() with map() vs slice() with Array.from()
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(Boolean);
slice() with Array.from()
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 = Array.from(transferArray, Boolean);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
readUint8() with for loop
slice() with map()
slice() with Array.from()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0
Browser/OS:
Firefox 132 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
readUint8() with for loop
22900.2 Ops/sec
slice() with map()
137948.8 Ops/sec
slice() with Array.from()
153412.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark that tests the performance of three different approaches to convert binary data ( Uint8Array ) to boolean values. **Approaches Compared** 1. **`readUint8() with for loop`**: This approach uses the `DataView.getUint8()` method to read individual bytes from the Uint8Array and then converts each byte to a boolean value using a `for` loop. 2. **`slice() with map()`**: This approach uses the `Array.prototype.slice()` method to create a new Uint8Array view, followed by the `map()` function to convert each byte to a boolean value. 3. **`slice() with Array.from()`**: This approach uses the `Array.prototype.slice()` method to create a new Uint8Array view and then converts each byte to a boolean value using the `Array.from()` method. **Pros and Cons of Each Approach** 1. **`readUint8() with for loop`**: * Pros: Simple, straightforward implementation. * Cons: May be slower due to the overhead of reading individual bytes and iterating over the array. 2. **`slice() with map()`**: This approach is often considered the most efficient way to convert binary data to boolean values in modern JavaScript engines. * Pros: Fast execution, as it uses the optimized `map()` function. * Cons: May require additional memory allocation for the intermediate slice array. 3. **`slice() with Array.from()`**: * Pros: Similar efficiency to the `map()` approach and avoids the need for an intermediate array. * Cons: Requires support for the `Array.from()` method, which may not be available in older browsers. **Library Used** None explicitly mentioned. However, the benchmark uses modern JavaScript features, such as `DataView`, `Array.prototype.slice()`, and `Array.from()`. **Special JS Features/Syntax** None mentioned. **Other Considerations** * The benchmark is designed to measure the performance of these approaches on a specific dataset (1000 bytes) and may not be representative of real-world use cases. * The benchmark results are dependent on the browser, device platform, and operating system used for execution. * Other factors that may impact performance, such as caching and optimization algorithms implemented by the JavaScript engine, are not accounted for in this benchmark. **Alternatives** Other approaches to converting binary data to boolean values might include: 1. Using bitwise operations (e.g., `byte & 0x80 === 0`). 2. Implementing a custom conversion function using SIMD instructions. 3. Leveraging hardware acceleration through WebAssembly or other low-level APIs. However, these alternatives are likely to be less efficient and may not be supported by all browsers or devices.
Related benchmarks:
Slice vs Splice delete
Slice vs Splice delete 1000
Slice vs Splice vs Shift for 1 item
Binary to boolean with DataView: readUint8() with for loop vs. slice() with map()
Comments
Confirm delete:
Do you really want to delete benchmark?