Skip to content

Optimize create.js Ticker with idle throttling and tab visibility handling #5800

Open
stutijain2006 wants to merge 2 commits intosugarlabs:masterfrom
stutijain2006:PerformanceIssues
Open

Optimize create.js Ticker with idle throttling and tab visibility handling #5800
stutijain2006 wants to merge 2 commits intosugarlabs:masterfrom
stutijain2006:PerformanceIssues

Conversation

@stutijain2006
Copy link
Contributor

This PR introduces performance optimizations to reduce unnecessary CPU usage when Music Blocks is idle or running in the background.

Previously-

  1. create.js ticker continued running at 60 FPS even when the user was inactive
  2. Rendered continued even when the tab was hidden
  3. This causes unnecessary main thread workload and CPU usage.

After this PR-

  1. Removes redundant ticker initialization from index.html
  2. Implemented controlled idle throttling by-
  • Active = 60 FPS
  • Idle (after 5 second inactivity) = 1 FPS
  1. Adds visibilitychange listener which -
  • Pauses ticker when tab is hidden
  • Resumes correctly when the tab becomes visible.
  1. Restores active FPS immediately when the interaction resumes.

Runtime Impact-
Before: Main Runtime thread : 8469 ms
Screenshot 2026-02-19 025910

After : Main Runtime thread : 7371 ms
Screenshot 2026-02-19 030427

That is a reduction of 1100 ms in main runtime workload during a 30 sec interaction session.

This is a runtime efficiency optimization- not a lighthouse score improvements leading to -

  • Reduced CPU usage
  • Lower battery consumption

@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

Copy link
Contributor

@kartikktripathi kartikktripathi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idle throttling and visibility handling work correctly in testing.
Verified:

  • No first interaction delay after idle
  • Playback timing remains stable
  • Background tab pause/resume works without tempo jumps
  • Rapid tab switching does not create duplicate playback
  • Ticker framerate restores correctly (60 → idle → 60)
  • npm run test was run, and passed all the tests

The optimisation reduces unnecessary runtime work and behaves safely.

One small note: the recursive setTimeout idle check isn’t cleaned up if Activity is reinitialised, which could potentially create multiple loops in future refactors. Not blocking for this PR.

LGTM, Thanks for the contribution!

@kartikktripathi
Copy link
Contributor

@walterbender, I believe this PR is ready to be merged.
Thanks!

@walterbender
Copy link
Member

Can you either rebase or run prettier on activity.js?

@stutijain2006
Copy link
Contributor Author

Can you either rebase or run prettier on activity.js?

Done

@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants