Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from() vs spread []
(version: 0)
Testing the difference between creating filled arrays.
Comparing performance of:
new Array() vs spread
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
new Array()
new Array(0, 1, 2)
spread
[0, 1, 2]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
new Array()
spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
17 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 Edg/147.0.0.0
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new Array()
10447842.0 Ops/sec
spread
29676416.0 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 benchmark is defined by two different approaches to create filled arrays: 1. `new Array(0, 1, 2)` 2. `[0, 1, 2]` These two approaches are compared to measure their performance differences. **Options Compared** The two options being compared are: * Using the constructor syntax (`new Array(0, 1, 2)`) to create a filled array. * Using the spread operator (`[0, 1, 2]`) to create a filled array. **Pros and Cons of Each Approach** **Constructor Syntax (New Array)** Pros: * Can be more readable and expressive, especially for larger arrays. * Allows for more control over the initialization process (e.g., initializing elements with default values). Cons: * May incur additional overhead due to the constructor's creation process. **Spread Operator ([0, 1, 2])** Pros: * Is concise and easy to read. * Does not incur the same overhead as the constructor syntax. Cons: * Less readable than the constructor syntax for larger arrays. * Limited control over initialization processes (e.g., default values). **Library Used: None** There is no library used in this benchmark. Both approaches are built-in JavaScript constructs. **Special JS Feature/Syntax:** No special JavaScript features or syntax are mentioned in the provided JSON. **Other Alternatives** Some alternative approaches to create filled arrays could be: * Using `Array.from()` with an array of initial values (e.g., `[...new Array(3)].map((_, i) => i)`). * Using a loop to initialize elements (e.g., `let arr = []; for (let i = 0; i < 3; i++) { arr.push(i); }`). These alternatives might offer performance advantages or more readable code, but they are not explicitly tested in this benchmark. Overall, the benchmark provides a simple comparison between two common approaches to create filled arrays in JavaScript, highlighting their pros and cons.
Related benchmarks:
Javascript string to array mapping: Array.from() vs Spread syntax [...spread]
Array.from() vs new Array()
Array.from() vs new Array() - empty
Array() vs Array.from() fill
Comments
Confirm delete:
Do you really want to delete benchmark?