function createDateArrayInAMonth(year, month) {
return Array.from(
{ length: new Date(year, month, 0).getDate() },
(_, 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()).fill().map((_, i) => new Date(year, month - 1, i + 1).toLocaleString())
}
createDateArrayInAMonth(2023, 10)
function createDateArrayInAMonth(year, month) {
return 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) {
const arr = []
for (let i = 0; i < new Date(year, month, 0).getDate(); i++) {
arr.push(new Date(year, month - 1, i + 1).toLocaleString())
}
return arr
}
createDateArrayInAMonth(2023, 10)
function createDateArrayInAMonth(year, month) {
const arr = []
for (let i = 0; i < new Date(year, month, 0).getDate(); i++) {
arr[i] = new Date(year, month - 1, i + 1).toLocaleString()
}
return arr
}
createDateArrayInAMonth(2023, 10)
function createDateArrayInAMonth(year, month) {
const date = new Date(year, month - 1, 1)
const arr = []
while (date.getMonth() === month - 1) {
arr.push(new Date(date).toLocaleString())
date.setDate(date.getDate() + 1)
}
return arr
}
createDateArrayInAMonth(2023, 10)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array From | |
new Array | |
Array | |
for push | |
for index | |
While |
Test name | Executions per second |
---|---|
Array From | 14252.9 Ops/sec |
new Array | 15203.5 Ops/sec |
Array | 14870.6 Ops/sec |
for push | 12916.7 Ops/sec |
for index | 12758.7 Ops/sec |
While | 14120.9 Ops/sec |
Overview of the Benchmark
MeasureThat.net is a website where users can create and run JavaScript microbenchmarks to compare the performance of different approaches in creating an array of dates within a specific month. The provided benchmark definition and test cases are designed to measure the execution speed of various methods for generating arrays of dates.
Test Cases
The benchmark consists of six test cases, each representing a different approach to create an array of dates:
Array.from()
method to create an array from a date range.fill()
method.fill()
.for
loop to iterate over a date range and add each date to an empty array using the push()
method.for
loop to iterate over the indices of the array instead of dates.while
loop to incrementally create an array of dates.Comparison of Approaches
Here's a brief analysis of each approach:
Array From
, but creates the array directly without using fill()
. It may be faster since it avoids the overhead of creating an empty array.while
loop, which can lead to slower execution.Libraries Used
There are no libraries explicitly mentioned in the benchmark definition or test cases. However, some libraries like Lodash (_
) might be used indirectly through its methods (e.g., Array.from()
).
Special JS Features/Syntax
The following special JavaScript features/syntax are used:
"${year}-${month}-${day}"
).(date) => ...
).Performance Insights
The provided benchmark results show that the execution speed of each approach varies:
Array From
and new Array
are relatively fast.Array
is likely faster than Array From
due to its direct array creation.for push
and for index
are slower than the previous approaches.While
is the slowest.These results might vary depending on the specific JavaScript engine, browser, or environment used.