Skip to content

Bug: Heavy timer loop in alarm challenge causes UI jank and high CPU #916

@NihalDR

Description

@NihalDR

Summary

The timer implementation in the alarm challenge performs a very large async loop that is expensive on the UI isolate.

Problematic Code

File: lib/app/modules/alarmChallenge/controllers/alarm_challenge_controller.dart

  • Around line 202: totalIterations = 1500000
  • Around line 204: loop iterates up to 1,500,000 times
  • Around line 217: await Future.delayed(duration ~/ i) runs in each iteration

Current pattern:

  • Performs 1.5M loop iterations
  • Schedules/delays every iteration
  • Continuously mutates reactive state (progress.value -= decrement)

Impact

  • High CPU usage during challenge timer
  • Potential UI jank/frame drops on lower-end devices
  • Unnecessary battery drain
  • Risk of degraded app responsiveness while alarm challenge is active

Expected Behavior

Timer/progress updates should be time-based and efficient (constant or low-frequency updates), without massive iteration counts on the UI isolate.

Suggested Fix

Replace the loop with a time-driven approach, for example:

  • Use Timer.periodic with a reasonable tick interval (e.g. 16ms/50ms/100ms), or
  • Use an AnimationController for smooth progress over 15 seconds, or
  • Compute progress from elapsed time using a Stopwatch and a low-frequency update stream.

Also:

  • Cancel/dispose timer cleanly in onClose
  • Guard against updates after controller disposal

Repro

  1. Enable timer challenge in alarm challenge flow
  2. Start challenge
  3. Observe increased CPU usage and possible UI stutter while timer runs

Acceptance Criteria

  • No large iteration async loop for timer progression
  • Timer remains functionally equivalent (15-second countdown)
  • Reduced CPU utilization and no noticeable UI jank during challenge

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions