Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
new Uint8Array() vs Uint8Array.from() reverse
(version: 0)
Test ArrayBuffer creation methods on a random Array
Comparing performance of:
Test Uint8Array.from() vs Test new Uint8Array()
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var randomArray = Array.from({ length: 1000 }, () => Math.floor(Math.random() * 256))
Tests:
Test Uint8Array.from()
Uint8Array.from(randomArray)
Test new Uint8Array()
new Uint8Array(randomArray)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Test Uint8Array.from()
Test new Uint8Array()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 16_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 16 on iOS 16.3.1
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Test Uint8Array.from()
80690.5 Ops/sec
Test new Uint8Array()
107662.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON benchmark. **What is being tested?** The benchmark compares two methods of creating a `Uint8Array` object: 1. `new Uint8Array()`: This method creates a new, empty `Uint8Array` object using the JavaScript constructor. 2. `Uint8Array.from()`: This method creates a new `Uint8Array` object from an iterable (in this case, a random array). **Options being compared** The two options are: 1. **Constructor-based creation**: `new Uint8Array()` (tested in one test case) * Pros: Can be more efficient for large arrays, as it allows the browser to use its internal buffer management. * Cons: Requires JavaScript code, which can slow down execution if not optimized. 2. **Array.from() method**: `Uint8Array.from()` (tested in another test case) * Pros: More concise and easier to read than constructor-based creation. * Cons: May be slower due to the overhead of using an additional method. **Library usage** There is no explicit library usage in this benchmark. However, it's worth noting that `Uint8Array` is a built-in JavaScript class, so no external library is required. **Special JS feature or syntax** The test case uses the `Array.from()` method, which was introduced in ECMAScript 2015 (ES6). This method creates a new array from an iterable by calling the specified callback function on each element of the original iterable. In this case, the callback function is simply returning the value of `Math.floor(Math.random() * 256)`, which generates random integers between 0 and 255. **Other alternatives** There are other ways to create a `Uint8Array` object in JavaScript: * Using a constructor with an array literal: `new Uint8Array([1, 2, 3])` * Using the `Uint8Array` prototype's `from()` method with an array literal: `Uint8Array.from([1, 2, 3])` However, these alternatives are not being tested in this benchmark. **Other considerations** When creating a `Uint8Array` object, it's essential to consider performance and memory efficiency. For large arrays, using the constructor-based creation method can be more efficient, as it allows the browser to use its internal buffer management. However, for smaller arrays or when readability is more important than performance, the `Array.from()` method can be a better choice. In this benchmark, the results indicate that `new Uint8Array()` might be slightly faster on iOS 16 devices, while `Uint8Array.from()` seems to have a slight advantage on other platforms.
Related benchmarks:
repeated Math.random() vs crypto.getRandomValues()
new Uint8Array() vs Uint8Array.from()
new Array() vs Array.from()
new Array() vs Array.from() with random data
Comments
Confirm delete:
Do you really want to delete benchmark?