Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from() vs new Array() performance...
(version: 0)
Testing the difference between creating filled arrays.
Comparing performance of:
new Array() vs Array.from()
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
new Array()
new Array(99999).fill(1).map((x, i) => ({a:1, b:2, c: i}))
Array.from()
Array.from({ length: 99999 }, (x, i) => ({a:1, b:2, c: i}))
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:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new Array()
1330.0 Ops/sec
Array.from()
462.3 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. The benchmark is designed to compare the performance of two approaches to create filled arrays: `new Array()` and `Array.from()`. The test cases are identical, except for the method used to create the array. **New Array()** `new Array()` creates a new JavaScript object that can be used as an array. When used with the `fill()` method, it fills the entire array with a specified value, in this case, `{a:1, b:2, c: i}`. The `map()` function is then applied to the filled array, which transforms each element into an object. Pros: * Simple and straightforward syntax * Can be used without any additional libraries or dependencies Cons: * Creates a new JavaScript object that can lead to memory allocations and garbage collection overhead * May not be as efficient as using native arrays (if available) **Array.from()** `Array.from()` is a static method on the `Array` prototype that creates a new array from an iterable source. In this case, it's used with an object literal `{ length: 99999 }`, which defines the size of the array to be created. The callback function `(x, i) => ({a:1, b:2, c: i})` is then applied to each element in the range, creating a new object for each iteration. Pros: * Can take advantage of native arrays and their optimized memory management * May be faster than `new Array()` due to reduced overhead Cons: * Requires the use of an additional method (`Array.from()`) and callback function * May require more complex syntax for those not familiar with it In the provided benchmark, both test cases are identical except for the method used to create the array. The resulting performance difference will depend on various factors, such as: 1. JavaScript engine optimizations: Different engines may optimize these methods differently. 2. Hardware and software configurations: Platform-specific features like Native Array support can affect performance. 3. Browser caching and rendering: The browser's ability to cache and render the benchmark results might influence execution times. In general, `Array.from()` might provide a slight performance advantage due to its native array benefits, but the actual difference may be negligible in many cases. However, if performance is critical, using `Array.from()` could be a better choice. **Other alternatives** Other approaches to create filled arrays include: * Using the `Array` constructor with arguments (`new Array(99999)`): This method creates an array directly and is equivalent to `new Array().fill()`. It's similar to `new Array()` but might have slightly different performance characteristics. * Using a loop to populate the array: Instead of using `map()` or `Array.from()`, you could use a simple loop to fill the array element by element. However, these alternatives are less commonly used and might not provide the same benefits as using native arrays or `Array.from()`.
Related benchmarks:
Array.from() vs new Array()
my Array.from() vs new Array()
Array.from() vs new Array() - empty
Array.from({ length: n }) vs new Array(n)
Array.from() vs new A
Comments
Confirm delete:
Do you really want to delete benchmark?