Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from({ length: n }) vs new Array(n)
(version: 0)
Testing the difference between creating filled arrays.
Comparing performance of:
new Array() vs Array.from()
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
new Array()
new Array(500)
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 year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new Array()
6366132.5 Ops/sec
Array.from()
99618.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The first section defines a benchmark, which is a measure of performance in software development. In this case, we're testing two approaches to create filled arrays: 1. `new Array(n)` 2. `Array.from({ length: n })` Both methods aim to create an array with `n` elements, filled with a default value (usually `undefined`). The main difference lies in the syntax and potential performance implications. **Options Compared** The benchmark compares two options: A) Using the traditional `new Array(n)` method. B) Using the `Array.from()` method with an object initializer. **Pros and Cons of Each Approach** 1. **`new Array(n)`** * Pros: + Wide browser support (all versions) + Simple syntax * Cons: + May create a large number of unnecessary objects in memory (each array is a separate object) + Can be slower for very large arrays due to the overhead of creating multiple objects 2. **`Array.from()` with an object initializer** * Pros: + More efficient than `new Array(n)` for large arrays, as it avoids creating unnecessary objects + Can handle more complex initialization patterns (e.g., `Array.from([1, 2], (_, i) => i * 2)`) * Cons: + Not supported in older browsers (Internet Explorer) + Requires the initialization object to be created **Other Considerations** The benchmark assumes that the initialization objects are empty (`{ length: n }`). This simplifies the comparison and focuses on the creation of filled arrays. If you wanted to compare more complex initialization patterns, the `Array.from()` method would provide more flexibility. **Library and Syntax Features** There is no specific library mentioned in this benchmark. However, if we consider `Array.from()`, it was introduced in ECMAScript 2015 (ES6) and provides a convenient way to create arrays from iterable sources or objects. **Special JS Feature or Syntax** None are explicitly mentioned in this benchmark. If you wanted to test specific features like async/await, Promises, or Web Workers, you would need to modify the benchmark definition accordingly. Now, let's discuss alternative approaches: * **Using `Array` constructor with a single argument**: This method is similar to `new Array(n)`, but it avoids creating an unnecessary object. However, it still has performance implications for very large arrays. * **Using `Array.from()` with a custom iterator**: Instead of using an empty object initializer, you could use a custom iterator function to create the array. This approach provides more flexibility and can be useful in specific scenarios. Keep in mind that benchmarking is an essential part of software development, as it helps identify performance bottlenecks and optimize code for different browsers and environments.
Related benchmarks:
Array.from() vs new Array()
Array.from() vs new Array() - empty
Array.from() vs new A
Array(length).fill() vs Array.from({ length: length })
Comments
Confirm delete:
Do you really want to delete benchmark?