Test name | Executions per second |
---|---|
Using Math | 2398260.8 Ops/sec |
Using Date Object | 1716883.1 Ops/sec |
Small Date Object | 1658333.9 Ops/sec |
Even smaller Date Object | 1747625.9 Ops/sec |
let startDate = new Date();
const getNextMonth = (startDate) => {
let current;
if (startDate.getMonth() == 11) {
current = new Date(startDate.getFullYear() + 1, 0, 1);
} else {
current = new Date(startDate.getFullYear(), startDate.getMonth() + 1, 1);
}
return current.getMonth();
};
let startDate = new Date();
getNextMonth(startDate);
const outputDate = (date, output = 'month') => {
switch (output) {
default:
case 'month':
return date.getMonth();
case 'fulldate':
return date;
case 'year':
return date.getFullYear();
}
};
const changeMonth = (current = new Date(), change) => {
if (change === 0) return current;
const unformatted = new Date(current).setMonth(current.getMonth() + change);
const formatted = new Date(unformatted);
return formatted;
};
const getNextMonth = (current = new Date(), output = 'month') => {
const nextMonth = changeMonth(current, 1);
return outputDate(nextMonth, output);
};
let startDate = new Date();
getNextMonth(startDate)
const changeMonth = (current, change) => {
if (change === 0) return current;
const unformatted = new Date(current).setMonth(current.getMonth() + change);
return new Date(unformatted);
};
const getNextMonth = (current) => {
return changeMonth(current, 1).getMonth()
};
let startDate = new Date();
getNextMonth(startDate)
const getNextMonth = (current) => {
const unformatted = new Date(current).setMonth(current.getMonth() + 1)
return new Date(unformatted).getMonth()
};
let startDate = new Date();
getNextMonth(startDate)