const start = new Date("2020-06-01");
const end = new Date("2026-05-01");
let dates = [];
for (let currentDate = start; currentDate <= end; currentDate.setMonth(currentDate.getMonth() + 1)) {
dates.push(currentDate.toISOString().split("T")[0]);
}
const start = new Date("2020-06-01");
const end = new Date("2026-05-01");
let dates = [];
for (let currentDate = start; currentDate <= end; currentDate.setMonth(currentDate.getMonth() + 1)) {
dates = [dates, currentDate.toISOString().split('T')[0]];
}
const start = new Date("2020-06-01");
const end = new Date("2026-05-01");
let dates = [];
let currentDate = start;
while (currentDate <= end) {
dates.push(currentDate.toISOString().split("T")[0]);
currentDate.setMonth(currentDate.getMonth() + 1);
}
const start = new Date("2020-06-01");
const end = new Date("2026-05-01");
let dates = [];
let currentDate = start;
while (currentDate <= end) {
dates = [dates, currentDate.toISOString().split('T')[0]];
currentDate.setMonth(currentDate.getMonth() + 1);
}
const start = new Date("2020-06-01");
const end = new Date("2026-05-01");
let dates = Array.from(
{ length: (end.getFullYear() - start.getFullYear()) * 12 },
(_, i) => new Date(start.getFullYear(), start.getMonth() + i, 1).toISOString().split('T')[0]
);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
For loop + Array.push | |
For loop + Spread Operator | |
While + Array.push | |
While + Spread Operator | |
Array.from |
Test name | Executions per second |
---|---|
For loop + Array.push | 11180.5 Ops/sec |
For loop + Spread Operator | 9885.8 Ops/sec |
While + Array.push | 11469.3 Ops/sec |
While + Spread Operator | 9992.7 Ops/sec |
Array.from | 10061.9 Ops/sec |
Benchmark Overview
The provided benchmark measures the performance of different approaches to create an array of Date ISO strings within a specified date range.
Options Compared
The benchmark compares five different approaches:
Array.from()
method to create an array of Dates within the specified date range, and then maps each Date to its ISO string.Pros and Cons
Here's a brief summary of the pros and cons for each approach:
Array.from()
and its options.Library and Special JS Feature
No special JavaScript features or libraries are used in this benchmark. The benchmark focuses on demonstrating different approaches to creating an array of Date ISO strings using built-in JavaScript methods.
Other Considerations
When optimizing performance-critical code, it's essential to consider factors such as:
In this benchmark, the approach that performs best will likely be one that balances these considerations effectively.