Date creation speed

5 months ago
User agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0
Test name Executions per second
Plain JS 7507373.5 Ops/sec
Day Class 6122255.0 Ops/sec
Script Preparation code:
x
 
class DayModel {
    constructor(year, month, date) {
        this.year = year
        this.month = month
        this.date = date
    }
    /**
     * Checks if the passed CalendarDate is equal to itself.
     */
    isEqual(day) {
        if (day) {
            return (
                this.year === day.year &&
                this.month === day.month &&
                this.date === day.date
            )
        }
        return false
    }
    toDate() {
        return new Date(this.year, this.month, this.date)
    }
    /**
     * Returns a new DayModel which is incremented based on the value passed.
     */
    incrementBy(value) {
        // Creating new Javascript Date object to increment because
        // it will automatically take care of switching to next or previous
        // months & years without we having to worry about it.
        const date = new Date(this.year, this.month, this.date + value)
        return new DayModel(date.getFullYear(), date.getMonth(), date.getDate())
    }
    /**
     * Clones the current day model.
     */
    clone() {
        return new DayModel(this.year, this.month, this.date)
    }
    toComparisonString() {
        return `${this.year}${this.pad(this.month)}${this.pad(this.date)}`
    }
    toDateString() {
        return this.toDate().toLocaleDateString(undefined, {
            weekday: "long",
            month: "long",
            day: "numeric",
            year: "numeric"
        })
    }
    /**
     * Compares the dates and returns boolean value based on the value passed
     */
    isBefore(day, dayInclusive = false) {
        return dayInclusive ?
            this.toDate().getTime() <= day?.toDate().getTime() :
            this.toDate().getTime() < day?.toDate().getTime()
    }
    /**
     * Compares the dates and returns boolean value based on the value passed
     */
    isAfter(day, dayInclusive = false) {
        return dayInclusive ?
            this.toDate().getTime() >= day?.toDate().getTime() :
            this.toDate().getTime() > day?.toDate().getTime()
    }
    pad(num) {
        return num < 10 ? `0${num}` : `${num}`;
    }
}
function getDaysInMonthJS(month, year) {
    const date = new Date(year, month, 1);
    const days = [];
    while (date.getMonth() === month) {
        days.push(new Date(date));
        date.setDate(date.getDate() + 1);
    }
    return days;
}
function getDaysInMonthTS(month, year) {
    const nbD = new Date(year, month + 1, 0).getDate();
    return Array(nbD)
      .fill(null)
      .map((_date, index) => {
        return new DayModel(year, month, index + 1);
      });
}
Tests:
  • Plain JS

     
    getDaysInMonthJS(2024, 1)
  • Day Class

     
    getDaysInMonthJS(2024, 1)
Open this result on MeasureThat.net