Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
new Array() vs Array.from()
(version: 3)
Test Array creation methods on a random ArrayBuffer
Comparing performance of:
new Array() vs Array.from() vs new Array() with ArrayBuffer vs Array.from() with ArrayBuffer
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var randomArray = Array.from({ length: 1000 }, () => Math.floor(Math.random() * 256)) var randomArrayBuffer = new Uint8Array(randomArray)
Tests:
new Array()
new Array(...randomArray)
Array.from()
Array.from(randomArray)
new Array() with ArrayBuffer
new Array(...randomArrayBuffer)
Array.from() with ArrayBuffer
Array.from(randomArrayBuffer)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
new Array()
Array.from()
new Array() with ArrayBuffer
Array.from() with ArrayBuffer
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Definition:** The benchmark tests different methods of creating an array in JavaScript, specifically comparing `Array.from()` with `new Array()` and variations that use a random ArrayBuffer as input. **Options Compared:** 1. **`Array.from(randomArray)`**: This method creates a new array by mapping over the `randomArray` and using `undefined` as the initial value. 2. **`new Array(...randomArray)`**: This method creates a new array by spreading the elements of `randomArray`. 3. **`new Array(...randomArrayBuffer)`**: This method creates a new array by spreading the elements of `randomArrayBuffer`. **Pros and Cons:** * **`Array.from()`**: + Pros: More concise, readable, and expressive syntax. + Cons: May have performance overhead due to the mapping operation. * **`new Array(...)`**: + Pros: Lightweight, fast, and suitable for large arrays. + Cons: Less readable and explicit than `Array.from()`, can lead to errors if not used carefully. **Using a Random ArrayBuffer:** The test case uses a random ArrayBuffer as input to simulate a more realistic scenario. This allows the benchmark to measure performance in different contexts, such as when working with large arrays or arrays generated from external data sources. **Library and Purpose:** In this benchmark, `Array.from()` is used as a library function to create an array from a given iterable (in this case, a random array). The purpose of using `Array.from()` is to test its performance compared to other methods. **Special JS Feature/Syntax:** There are no special JavaScript features or syntaxes mentioned in the benchmark definition. However, it's worth noting that `Array.from()` was introduced in ECMAScript 2015 (ES6), and the benchmark may be using a version of JavaScript that supports this feature. **Other Alternatives:** * **Using `new Uint8Array(...)`**: Instead of creating an array from a random ArrayBuffer, the test case could use `new Uint8Array()` to create a typed array. This would allow testing performance with a specific type of array. * **Using other array creation methods**: The benchmark could be extended to include other array creation methods, such as using `Array.prototype.push()` or `Array.prototype.fill()`, to provide more comprehensive results. Overall, the benchmark provides a useful test case for measuring performance differences between various array creation methods in JavaScript.
Related benchmarks:
repeated Math.random() vs crypto.getRandomValues()
new Uint8Array() vs Uint8Array.from()
new Array() vs Array.from() with random data
new Uint8Array() vs Uint8Array.from() reverse
Comments
Confirm delete:
Do you really want to delete benchmark?