Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
new Array() vs Array.from() with random data
(version: 0)
Comparing performance of:
new Array(1000) vs Array.from({length: 1000})
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
new Array(1000)
new Array(1000).fill(0).map(() => Math.random())
Array.from({length: 1000})
Array.from({length: 1000}, () => Math.random())
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
new Array(1000)
Array.from({length: 1000})
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new Array(1000)
5220.2 Ops/sec
Array.from({length: 1000})
3459.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark measures the performance difference between creating an array using `new Array()` versus using `Array.from()` with random data. **What is being tested?** Two test cases are compared: 1. Creating an array of 1000 elements using `new Array(1000).fill(0).map(() => Math.random())`. 2. Creating an array of 1000 elements using `Array.from({length: 1000}, () => Math.random())`. **Options being compared** The two approaches are compared in terms of their performance, which is measured by the number of executions per second. **Pros and Cons of each approach** 1. **Using `new Array()`**: * Pros: + This method creates a new array with a specific length, which can be useful when the length is known beforehand. * Cons: + It involves additional overhead due to the creation of an array, which may not be necessary if only a few elements are needed. 2. **Using `Array.from()`**: * Pros: + This method creates an array with a specific length and fills it with values in a single operation, which can be more efficient than using `new Array()`. * Cons: + It requires the creation of an object with a `length` property, which may not be necessary if only an array is needed. **Library used** In this benchmark, no specific library is used beyond what is built-in to JavaScript. However, it's worth noting that `Array.from()` was introduced in ECMAScript 2015 (ES6) as part of the standard library. **Special JavaScript features or syntax** None are mentioned in this benchmark. Both test cases use basic JavaScript syntax and do not rely on any advanced features or libraries beyond what is built-in to JavaScript. **Other alternatives** There are other ways to create arrays in JavaScript, such as using a `for` loop: ```javascript const arr = new Array(1000); for (let i = 0; i < 1000; i++) { arr[i] = Math.random(); } ``` Or using `Array.prototype.fill()` and `Math.random()`: ```javascript const arr = new Array(1000).fill(0).map(() => Math.random()); ``` However, these approaches are not what the benchmark is comparing (i.e., `new Array()` vs. `Array.from()`)
Related benchmarks:
Array.from() vs new Array()
new Uint8Array() vs Uint8Array.from()
Array.from() vs new A
new Uint8Array() vs Uint8Array.from() reverse
Comments
Confirm delete:
Do you really want to delete benchmark?