function createDateArrayInAMonth(year, month) {
return new Array(new Date(year, month, 0).getDate()).fill().map((_, i) => new Date(year, month - 1, i + 1).toLocaleString())
}
createDateArrayInAMonth(2023, 10)
function createDateArrayInAMonth(year, month) {
return Array.apply(null, new Array(new Date(year, month, 0).getDate())).map((_, i) => new Date(year, month - 1, i + 1).toLocaleString())
}
createDateArrayInAMonth(2023, 10)
function createDateArrayInAMonth(year, month) {
return [new Array(new Date(year, month, 0).getDate())].map((_, i) => new Date(year, month - 1, i + 1).toLocaleString())
}
createDateArrayInAMonth(2023, 10)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array Fill | |
Array Apply | |
Array Spread |
Test name | Executions per second |
---|---|
Array Fill | 14924.8 Ops/sec |
Array Apply | 14761.7 Ops/sec |
Array Spread | 14646.1 Ops/sec |
Overview
The provided JSON represents a JavaScript benchmark test case on the MeasureThat.net website. The test compares three different approaches for creating an array of dates in a given month: Array Fill
, Array Apply
, and Array Spread
. Each approach is measured and compared to determine which one performs best.
Approaches Compared
fill()
method to initialize an empty array with a specified length, followed by mapping over the array to create dates.apply()
method to call a function on an array object, passing the desired number of elements as the first argument....
) to create an array with a specified length, followed by mapping over the array to create dates.Pros and Cons of Each Approach
fill()
method.Library Usage
None of the test cases use any external libraries or dependencies.
Special JS Feature/Syntax
The map()
function is used in all three approaches, which is a modern JavaScript feature that allows transforming arrays into new arrays. The toLocaleString()
method is also used to format dates as strings.
Benchmark Results
The latest benchmark results show the following:
Test Name | Executions Per Second |
---|---|
Array Fill | 14924.818359375 |
Array Apply | 14761.6669921875 |
Array Spread | 14646.08984375 |
The results indicate that Array Fill
performs best, followed by Array Apply
, and then Array Spread
.
Alternative Approaches
Other approaches to creating an array of dates in a given month might include:
Array.from()
method (a more modern and concise alternative to apply()
)