Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from() vs new Array() vs push pushup
(version: 0)
Testing the difference between creating filled arrays.
Comparing performance of:
new Array() vs Array.from() vs push
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
new Array()
new Array(500).fill("push-up", 0, 500)
Array.from()
Array.from({ length: 500 }, () => "push-up")
push
let array = []; for (let i = 0; i < 500; i++){ array.push("push-up") }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
new Array()
Array.from()
push
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new Array()
609595.7 Ops/sec
Array.from()
27223.9 Ops/sec
push
465561.6 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, compared options, pros and cons, and other considerations. **Benchmark Overview** The test cases aim to compare the performance of three approaches for creating filled arrays in JavaScript: 1. `new Array()` 2. `Array.from()` 3. `push` method (manual array creation) **Test Cases Breakdown** 1. **`new Array(500).fill("push-up", 0, 500)`**: This test case creates a new array with 500 elements, filling it with the string "push-up". The first parameter to `Array.fill()` sets the initial value, while the second parameter specifies the number of times to repeat that value. 2. **`Array.from({ length: 500 }, () => "push-up")`**: This test case uses the `Array.from()` method with a generator function that returns the string "push-up". The `{ length: 500 }` object defines the length of the resulting array. 3. **`let array = []; for (let i = 0; i < 500; i++) { array.push("push-up"); }`**: This test case creates an empty array and then uses a `for` loop to push the string "push-up" onto the array 500 times. **Options Comparison** The three approaches have different characteristics: * **`new Array()`**: Creates a new, empty array and can be optimized by the engine. This approach is simple and easy to read but might not be as efficient. * **`Array.from()`**: A more modern approach that uses a factory function (the generator function in this case) to create an array. This method is often faster than `push` because it avoids creating temporary variables and can utilize various optimizations provided by the engine. * **`push` method**: A manual approach that creates an empty array and then pushes elements onto it using the `push()` method. This approach requires more code and may not be as efficient due to the creation of a new array object and the need for multiple assignments. **Pros and Cons** * **`new Array()`**: + Pros: Simple, easy to read + Cons: Might not be optimized by the engine * **`Array.from()`**: + Pros: Often faster than `push`, can utilize optimizations + Cons: Requires modern JavaScript (ES6+) and a factory function * **`push` method**: + Pros: Simple, easy to understand for some developers + Cons: More code required, may not be optimized **Other Considerations** * The test cases use Chrome 122 as the browser. Different browsers might have varying performance due to engine optimizations or implementation differences. * The test cases are run on a desktop platform. Mobile devices and other platforms might have different performance characteristics. **Alternatives** If you're interested in exploring alternative approaches, consider: * Using `new Array()` with `fill()` instead of `Array.from()` * Implementing a custom array creation function using `Buffer` or `TypedArray` * Using other modern JavaScript features like `Map` or `Set` for array-like objects Keep in mind that the performance differences between these approaches may vary depending on the specific use case and environment.
Related benchmarks:
Array.from() vs new Array()
Array.from() vs new Array() - empty
Array.from() vs new A
Array.from() vs new Array() vs push
Comments
Confirm delete:
Do you really want to delete benchmark?