Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from() vs new Array() - empty
(version: 0)
Testing the difference between creating filled arrays.
Comparing performance of:
new Array() vs Array.from()
Created:
3 years ago
by:
Guest
Go to the latest result
Tests:
new Array()
new Array(500).fill({})
Array.from()
Array.from({ length: 500 })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
new Array()
Array.from()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36
Browser/OS:
Chrome 149 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
new Array()
2.1M/s
Array.from()
87K/s
View exact numbers
Test name
Executions per second
✓
new Array()
2,057,177 Ops/sec
Array.from()
87,033 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into explaining the benchmark. **What is tested:** The provided benchmark compares two approaches to create filled arrays in JavaScript: 1. `new Array()`: This method creates an empty array and then fills it with a specified value using the `fill()` method. 2. `Array.from()`: This method creates a new array from an iterable (in this case, an object) and fills it with a specified value. **Options compared:** The benchmark compares the performance of these two approaches: * `new Array()` with `new Array(500).fill({})` as the test case for creating filled arrays. * `Array.from()` with `Array.from({ length: 500 })` as the test case for creating filled arrays. **Pros and Cons:** 1. **`new Array()`:** * Pros: + Simple and straightforward way to create an array. + Can be used to create arrays of any size, not just fixed sizes. * Cons: + May have performance overhead due to the need to fill the entire array with a value. + Requires additional code to fill the array, which can add complexity. 2. **`Array.from()`:** * Pros: + More concise and expressive way to create arrays from iterables. + Can be used to create arrays of any size, not just fixed sizes. + Typically has better performance than `new Array()` due to the optimized implementation. * Cons: + Requires knowledge of the `Array.from()` method, which may not be familiar to all developers. **Library and purpose:** `Array.from()` is a built-in JavaScript method introduced in ECMAScript 2015 (ES6). Its purpose is to create a new array from an iterable source, such as an object or an array. In this benchmark, `Array.from()` is used to create a new array with a specified length and filled value. **Special JS feature:** The benchmark uses the `fill()` method, which is not a special JavaScript feature per se, but rather a part of the language that allows arrays to be filled with a specific value. However, some developers might be familiar with it as an optimization technique for creating large arrays. **Other considerations:** * The benchmark only tests the performance difference between `new Array()` and `Array.from()` when creating filled arrays. It does not account for other use cases where these methods are used. * The test cases use fixed sizes (500) to simplify the comparison, but in real-world scenarios, sizes may vary. **Alternatives:** Other alternatives to create filled arrays include: * Using a loop to create and fill an array: ```javascript const arr = []; for (let i = 0; i < 500; i++) { arr.push({}); } ``` * Using the `Array.from()` method with an empty object as the iterable source, like this: ```javascript const arr = Array.from({ length: 500 }, () => {}); ``` These alternatives may have different performance characteristics and trade-offs compared to `new Array()` and `Array.from()`.
Related benchmarks
Array.from() vs new Array()
my Array.from() vs new Array()
Array() vs Array.from() fill
Comments
Confirm delete:
Do you really want to delete benchmark?