Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs Array.fill (zeroes)
(version: 0)
Comparing performance of:
from vs fill
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
from
const n = 10000, length = 50 const result = [] for (let i = 0; i < n; ++i) { result.push(Array.from({length}, () => 0)) }
fill
const n = 10000, length = 50 const result = [] for (let i = 0; i < n; ++i) { result.push(Array(length).fill(0)) }
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:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
from
67.7 Ops/sec
fill
1172.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its components. **Benchmark Definition** The benchmark is defined in JSON format, specifying: 1. **Name**: The name of the benchmark, which is "Array.from vs Array.fill (zeroes)". 2. **Description**: An empty string, indicating that no description is provided for this benchmark. 3. **Script Preparation Code**: An empty string, meaning no code needs to be executed before running the benchmark. 4. **Html Preparation Code**: Another empty string, suggesting that no HTML code needs to be prepared for the benchmark. **Individual Test Cases** The benchmark consists of two individual test cases: 1. **Test Case 1: "from"** * The benchmark definition is a JavaScript script that creates an array using `Array.from()` and pushes it into another array. * The script uses a loop to push 50 elements (with length 50) into the result array, with a total of 10,000 iterations (defined by the constant `n`). 2. **Test Case 2: "fill"** * The benchmark definition is similar to Test Case 1, but instead uses `Array.fill()` to fill an array with zeros. **Library and Purpose** Neither of these test cases explicitly uses a library. However, it's worth noting that both `Array.from()` and `Array.fill()` are part of the ECMAScript standard (JavaScript), so no external library is required to run these benchmarks. **Special JS Feature or Syntax** There isn't any special JavaScript feature or syntax being used in this benchmark. Both `Array.from()` and `Array.fill()` are widely supported by modern browsers and Node.js environments. **Options Compared** The two test cases differ only in the method used to create the array: * Test Case 1 uses `Array.from()`, which takes an initial value (an object with a `length` property) as an argument. * Test Case 2 uses `Array.fill()`, which takes a value to fill with as an argument. **Pros and Cons of Different Approaches** Here's a brief analysis of the pros and cons of each approach: 1. **`Array.from()`** * Pros: + More flexible, allowing for custom creation of arrays. + Can be used to create arrays from other data structures (e.g., sets, maps). * Cons: + May have a slight performance overhead due to the object creation step. 2. **`Array.fill()`** + Pros: + Typically faster than `Array.from()`, as it only involves filling the array with zeros. + Cons: + Less flexible, as it requires knowing the length of the array in advance. **Other Considerations** In general, both methods are suitable for creating arrays filled with zeros or other values. The choice between them might depend on personal preference or specific use cases. However, if performance is a critical factor, `Array.fill()` might be slightly faster due to its simplicity. As for alternatives, you could consider using other methods like: * `new Array(length)` (a simpler and more lightweight approach) * `Array.prototype.map()` with a function that returns zeros (although this method may incur additional overhead) Keep in mind that the performance differences between these approaches are usually small, and the choice ultimately depends on your specific use case and requirements.
Related benchmarks:
toFixed vs Math.round() - result as a number
toFixed() vs Math.round().toString()
parseFloat(toFixed) vs Math.round()
toFixed vs Math.round() with numbers
fill test
Comments
Confirm delete:
Do you really want to delete benchmark?