Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from() vs new Array() vs []
(version: 0)
Testing the difference between creating filled arrays.
Comparing performance of:
new Array() vs Array.from() vs []
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
new Array()
const a = new Array(0)
Array.from()
const a = Array.from({ length: 0 })
[]
const a = []
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
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 (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()
6777215.0 Ops/sec
Array.from()
3081572.0 Ops/sec
[]
91561192.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares three different ways to create an empty array in JavaScript: 1. Using `new Array()` 2. Using `Array.from({ length: 0 })` 3. Using a raw array literal `[]` **Comparison of Options** Each option has its own pros and cons: * **`new Array()`**: This method creates a new, empty array object. It's a simple and concise way to create an array. However, it can lead to unexpected behavior if the array is used as a primitive value (e.g., passed as an argument to `String()`) or if it's not properly initialized. * **`Array.from()`**: This method creates a new array from an iterable source, such as an empty object with a `length` property. It provides more flexibility than `new Array()` and can handle more complex scenarios. However, it requires the source to be an object with a `length` property, which might not always be available. * **Raw Array Literal `[]`**: This method creates an array literal by writing `[]`. It's simple and concise but can lead to confusion if the syntax is not well-known. **Library Usage** None of the benchmark test cases explicitly use any libraries. However, `Array.from()` uses the `Array.prototype.from()` method, which is a part of the ECMAScript standard. The other two methods do not rely on any specific library. **Special JavaScript Features or Syntax** There are no special JavaScript features or syntax being tested in this benchmark. **Other Alternatives** Some alternative ways to create an empty array include: * Using `Array()`: This method creates a new, empty array object. However, it's less common and less preferred than the other methods. * Using `void(0).slice()`: This method creates a new, empty array by calling `slice()` on `void(0)`, which returns an empty array. * Using `Array.create()`: This method is similar to `new Array()`, but it's less common and less preferred. **Benchmark Preparation Code** Since the benchmark preparation code is null, it means that the JavaScript engine is responsible for initializing the test environment, and the benchmarks are executed in a headless scenario without any external dependencies.
Related benchmarks:
Array.from() vs new Array()
my Array.from() vs new Array()
Array.from() vs new Array() - empty
Array.from() vs new A
Comments
Confirm delete:
Do you really want to delete benchmark?