DEV Community

SEN LLC
SEN LLC

Posted on

A Sleep Cycle Calculator That Accounts for Fall-Asleep Time

A Sleep Cycle Calculator That Accounts for Fall-Asleep Time

Sleep cycles are 90 minutes. Waking up mid-cycle feels terrible; waking at a cycle boundary feels refreshing. This tool calculates optimal bedtimes and wake-up times, accounting for the 15 minutes (configurable) it takes to fall asleep — because "go to bed at 10:00" and "fall asleep at 10:00" are different things.

Most sleep calculators show you multiples of 90 minutes. But they forget that you don't fall asleep instantly. If you need to wake at 7:00 and it takes you 15 minutes to fall asleep, the optimal 5-cycle bedtime isn't 23:30 — it's 23:15.

🔗 Live demo: https://sen.ltd/portfolio/sleep-alarm/
📦 GitHub: https://github.com/sen-ltd/sleep-alarm

Screenshot

Features:

  • Two modes: "I want to wake at X" / "I'm going to bed at X"
  • 3-6 cycle options with quality labels
  • Configurable fall-asleep time (0-30 min)
  • Sleep phase visualization
  • Midnight-crossing support
  • Japanese / English UI
  • Zero dependencies, 42 tests

The math

export function calculateBedtimes(wakeUpTime, fallAsleepMin = DEFAULT_FALL_ASLEEP) {
  const results = [];
  for (let cycles = 6; cycles >= 3; cycles--) {
    const sleepMinutes = cycles * CYCLE_DURATION;
    const totalMinutes = sleepMinutes + fallAsleepMin;
    const bedtime = new Date(wakeUpTime.getTime() - totalMinutes * 60000);
    results.push({
      bedtime,
      cycles,
      sleepHours: cycleSleepHours(cycles),
      quality: qualityLabel(cycles),
    });
  }
  return results;
}
Enter fullscreen mode Exit fullscreen mode

For wake-up at 07:00 with 15-min fall-asleep time:

  • 6 cycles (9.0h): go to bed at 21:45
  • 5 cycles (7.5h): go to bed at 23:15 ← optimal for most
  • 4 cycles (6.0h): go to bed at 00:45
  • 3 cycles (4.5h): go to bed at 02:15

Quality labels

Not all cycle counts are equal:

export function qualityLabel(cycles) {
  if (cycles >= 5) return 'optimal';
  if (cycles === 4) return 'good';
  return 'fair';
}
Enter fullscreen mode Exit fullscreen mode

5-6 cycles (7.5-9h) is what sleep research recommends. 4 cycles (6h) is functional. 3 cycles (4.5h) is survival.

Series

This is entry #40 in my 100+ public portfolio series.

Top comments (0)