Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.fill VS array.push
(version: 0)
Comparing performance of:
array.push vs array.fill
Created:
3 years ago
by:
Registered User
Jump to the latest result
Tests:
array.push
const array = []; const n = 100000; for (let i = 0; i < n; i++) { array.push('#123456'); }
array.fill
const n = 100000; const array = Array(n).fill('#123456');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array.push
array.fill
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.push
1513.5 Ops/sec
array.fill
3893.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the JavaScript benchmark you provided. **What is being tested?** The two test cases are comparing the performance of two different ways to fill an array with a large number of elements: using the `fill()` method versus pushing individual elements onto the array using the `push()` method. The goal is to determine which approach is faster and more efficient for filling large arrays. **Options compared:** There are only two options being tested: 1. **`array.fill()`**: This method fills all elements of an array with a specified value. 2. **`array.push()```**: This method adds one or more elements to the end of an array and returns the new length of the array. **Pros and Cons:** * `array.fill()`: Pros: + Faster, as it only requires a single operation to fill all elements. + More efficient in terms of memory usage, as it doesn't require creating a large number of intermediate elements. Cons: + Limited flexibility, as the entire array is filled with the same value. * `array.push()`: Pros: + More flexible, as each element can be different. + Can be useful when working with dynamic data or arrays that need to be modified frequently. Cons: + Slower, as it requires a separate operation for each element. + Less efficient in terms of memory usage, as it creates intermediate elements. **Library and purpose:** There is no explicit library mentioned in the benchmark definition. However, `Array.prototype.fill()` is a standard method provided by the JavaScript array prototype, which allows you to fill an array with a specified value. **Special JS feature or syntax:** The test case doesn't explicitly mention any special JavaScript features or syntax, but it does rely on modern JavaScript features like arrow functions (`=>`) and template literals (`\r\n`). **Other considerations:** * The benchmark is designed to measure the performance of filling large arrays, which may not be a common use case in everyday programming. * The test results are likely biased towards modern browsers, as they tend to optimize for performance and efficiency. **Alternative approaches:** Other alternative approaches to filling an array could include: 1. Using `Array.prototype.splice()`: This method allows you to remove or add elements to an array at a specified index, which can be useful in certain situations. 2. Using a library like `lodash`'s `fill()` function: This function provides more flexibility and customization options compared to the built-in `Array.prototype.fill()` method. 3. Using a loop with `new Array(n).map()`: This approach uses the `map()` method to create a new array filled with elements, which can be useful when working with dynamic data. Keep in mind that these alternative approaches may have different performance characteristics and trade-offs compared to the original benchmark test.
Related benchmarks:
array update push vs spread
Array fill method vs push in for loop
Array.from() vs new Array() vs push
Spread vs Push when adding into array
Array.from() vs new Array() vs push pushup
Comments
Confirm delete:
Do you really want to delete benchmark?