Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs Array.fill Simple
(version: 0)
Comparing performance of:
from vs fill
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
from
Array.from({length: 10000}, (_, i) => i)
fill
Array(10000).fill(0).map((_,i)=>i)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
from
fill
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
from
4203.7 Ops/sec
fill
40010.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Definition:** The benchmark definition is a JSON object that describes the general characteristics of the test. In this case, there is no script preparation code or HTML preparation code, which means that the script itself will be run multiple times to gather data. **Test Cases:** There are two individual test cases: 1. **"from" Test Case:** The benchmark definition for this test case is `Array.from({length: 10000}, (_, i) => i)`. This creates an array of 10,000 elements using the `Array.from()` method. 2. **"fill" Test Case:** The benchmark definition for this test case is `Array(10000).fill(0).map((_,i)=>i)`. This creates an array of 10,000 elements by filling an array with zeros and then mapping over it to create a new array of indices. **Comparison of Approaches:** 1. **`Array.from()` vs `Array.fill()` and `.map()`**: * **Pros of `Array.from()`**: It is more concise and expressive, as it allows you to specify the initial elements of the array. * **Cons of `Array.from()`**: It may be slower due to the overhead of creating an object and then iterating over its properties. * **Pros of `Array.fill()` and `.map()`**: They are often faster and more efficient, as they use optimized C++ code under the hood. However, they require more manual setup and are less readable. 2. **Performance Comparison:** The benchmark results show that the "fill" test case is faster than the "from" test case. This may be due to the optimized C++ code used in the `Array.fill()` and `.map()` methods. **Libraries Used:** None of the test cases use any external libraries. **Special JS Features or Syntax:** The benchmark uses a few advanced JavaScript features: 1. **Arrow functions (`=>`)**: Arrow functions are a concise way to define small, single-expression functions. 2. **Template literals (`${}`)**: Template literals are a feature of modern JavaScript that allows you to embed expressions inside string literals. **Other Alternatives:** If you need to create an array with 10,000 elements using `Array.from()`, you can use: * `Array.from(Array(10000))` * `Array.from({length: 10000})` However, these alternatives may not be as concise or expressive as the original `Array.from()` method. Overall, the benchmark highlights the performance differences between using `Array.from()` and `Array.fill()` with `.map()`, and provides a useful insight into the trade-offs of different coding approaches.
Related benchmarks:
Array.from() vs new Array()
my Array.from() vs new Array()
Array.from() vs new Array() - empty
Array(length).fill() vs Array.from({ length: length })
Array() vs Array.from() fill
Comments
Confirm delete:
Do you really want to delete benchmark?