Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array vs UInt8Array resizing
(version: 2)
Comparing performance of:
Uint8Array vs Array
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
c = new Uint8Array([255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255])
Tests:
Uint8Array
let stream = new Uint8Array() let a = 1000 while (a --> 0) { const buf = new Uint8Array(stream.length + c.length); buf.set(stream, 0); buf.set(c, stream.length); stream = buf; }
Array
let stream = [] let a = 1000 const push = Array.prototype.push; while (a --> 0) { push.apply(stream, c) new Uint8Array(stream) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Uint8Array
Array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
26 days ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 146 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Uint8Array
108.1 Ops/sec
Array
64.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its options. **Benchmark Overview** The benchmark is designed to compare the performance of two approaches for resizing arrays in JavaScript: `Uint8Array` and native JavaScript `Array`. The test aims to determine which approach is faster and more efficient. **Script Preparation Code** The script preparation code creates a constant array `c` containing 20 values (represented as `255`, which is the maximum value for an unsigned 8-bit integer). **Benchmark Test Cases** There are two individual test cases: 1. **Uint8Array**: This test case creates a new `Uint8Array` instance and resizes it by concatenating another `Uint8Array` instance (`stream`) with the constant array `c`. The resulting array is then reassigned to the original `stream` variable. 2. **Array**: This test case uses the `push()` method of native JavaScript arrays to add elements from the constant array `c` to an empty array `stream`. After each iteration, a new `Uint8Array` instance is created and assigned to `stream`. **Library Used** The `Uint8Array` class is used in the first test case. This is a built-in JavaScript class that provides a typed array for representing 8-bit unsigned integers. **Special JS Feature/Syntax** None are explicitly mentioned in the benchmark code, but it's worth noting that modern JavaScript engines have various optimizations and features that may impact performance, such as: * Just-In-Time (JIT) compilation * Garbage collection * SIMD instructions However, these effects are usually subtle and not directly related to the specific syntax or features used in this benchmark. **Performance Considerations** When comparing the two approaches: * **Uint8Array**: This method uses a typed array, which can lead to faster performance due to: + Reduced memory allocation and deallocation overhead. + Improved cache locality. + Faster iteration and indexing operations. * **Array**: This method uses the `push()` method, which may incur additional overhead due to: + Slower memory allocation and reallocation during each push operation. + Increased garbage collection frequency. However, modern JavaScript engines are optimized for native array operations, and the performance difference between these two approaches is likely to be small in practice. **Other Alternatives** There are other ways to resize arrays in JavaScript: * Using `concat()` method: `stream = stream.concat(c)` * Using `set()` method with a buffer-like object (e.g., `new ArrayBuffer()`) * Using third-party libraries like `lodash` or `array-clone` These alternatives may offer different performance characteristics, but they are not part of the benchmarked test cases. Overall, the benchmark is designed to compare two specific approaches for resizing arrays in JavaScript and determine which one performs better. The results can help developers understand the trade-offs involved in using typed arrays versus native arrays, depending on their use case and performance requirements.
Related benchmarks:
Maping BooleanArray vs uInt8 Array2 vs uint8 with bitMasking _2
Extending ArrayBufferView
Instantiation of ArrayBuffer vs Normal Part 3
JS number to UInt8Array 64-bit little endian
Uint(8/16/32)Array and BigInt64Array comparison performance
Comments
Confirm delete:
Do you really want to delete benchmark?