Script Preparation code:
AخA
 
let startDate = new Date();
Tests:
  • Using Math

    x
     
    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);
  • Using Date Object

     
    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)
  • Small Date Object

     
    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)
  • Even smaller Date Object

     
    const getNextMonth = (current) => {
      const unformatted = new Date(current).setMonth(current.getMonth() + 1)
      return new Date(unformatted).getMonth()
    };
    let startDate = new Date();
    getNextMonth(startDate)
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Using Math
    Using Date Object
    Small Date Object
    Even smaller Date Object

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 2 years ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36
Chrome 107 on Windows
View result in a separate tab
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