Fix UX Lag: Using the PerformanceObserver API to Optimizing Web Performance
date
Aug 17, 2024
slug
optimizing-web-performance-using-the-performanceobserver-api-to-fix-animation-lag
status
Published
tags
performance
webdevelopment
javascript
ux
logging
summary
type
Post

When it comes to making your app run faster and smoother, logging is one of the most important steps. Think of it like this: Imagine driving a car without a speedometer, fuel gauge, or any indicator lights — how would you know when something’s wrong? Similarly, without proper logging in your app, it’s tough to tell where things are slowing down. Logging lets you see exactly where the issues are, so you can fix them before users even notice.
One of the trickiest performance issues to detect is when your app’s animations or interactions feel “laggy” or “janky.” This is where the PerformanceObserver API can be a lifesaver. It allows you to track how well your app is performing during animations and interactions, providing the data you need to troubleshoot and improve your app’s speed and responsiveness.
In this article, we’ll break down how to use the PerformanceObserver API to track and fix animation performance issues. Plus, we’ll suggest ways to visualize the performance data so you can see improvements more clearly.
Why Monitoring Animations is Key to Good UX
Ever scrolled through a website and noticed that the animations or transitions seem choppy? Maybe a dropdown menu didn’t open smoothly or an image carousel stuttered when you tried to navigate through it. This is a sign of poor performance during animations, and it can lead to a frustrating user experience (UX).
While tools like Core Web Vitals focus on page loading and layout stability, they don’t cover how well your app runs during user interactions. This is where the Long Animation Frame Timing feature of the PerformanceObserver API comes into play. It tracks when animations or interactions take longer than they should, so you can identify and fix the specific parts of your app that are slowing down.
How the PerformanceObserver API Helps
The PerformanceObserver API makes it easy to log and track animation performance issues. It listens for “long animation frames” — those moments when your app takes too long to render a frame, making the animations feel slow or janky. You can set a threshold for what you consider “too long” (e.g., anything longer than 150 milliseconds), and the API will automatically log whenever this happens.
Let’s look at a simple example of how you can use this API:
In this code:
- The PerformanceObserver listens for long frames (frames that take longer than 150ms).
- Whenever it detects a long frame, it logs the event, helping you track down where the lag happens.

Laggy UI vs Smooth UI
How Does This Help You?
Imagine your app has a fancy dropdown animation that sometimes lags, but you’re not sure why. By using this API, you could detect exactly when that lag occurs and pinpoint which animation or transition is causing it. You could even set up the logging to send the data to an analytics platform, where you can review performance data across multiple sessions and users.
Key Data the API Provides
The PerformanceObserver gives you detailed data about each animation frame that takes too long to render. Here are the key pieces of information:
entry.duration
: The duration of the animation frame in milliseconds. This tells you how long it took to render that specific frame. If the number is higher than expected (e.g., above 16ms for a 60fps target), then something in that animation is slowing down your app.
entry.startTime
: The exact time the animation frame started. This helps you correlate performance issues with specific user actions or times during the session.
entry.name
: Typically"long-animation-frame"
, this field confirms that the entry is related to a long animation frame.
This data can help you detect patterns, like whether certain interactions (e.g., opening a menu or navigating through a slider) consistently lead to performance problems.
How to Use This Data to Improve Performance
Once you’ve started logging long animation frames, you can use the data to improve your app. Here’s how:
- Detect Problematic Animations: The data will help you identify which animations are causing performance drops. For example, you might find that your dropdown menus are consistently causing long frames because of complex CSS transitions.
- Optimize JavaScript and CSS: If you detect frequent long frames, it’s time to optimize your code. This could mean simplifying CSS animations, reducing JavaScript execution time during interactions, or offloading heavy computations to background processes like web workers.
- Continuous Monitoring: By continually monitoring long frames, you can catch new performance issues early on, especially after rolling out new features or changes. Over time, you can fine-tune your app to deliver a smooth, responsive user experience.
Visualizing Your Performance Data
To make the performance data easier to understand and act on, you can visualize it. Consider using:
- Line Charts: Display long frame durations over time to spot trends and patterns.
- Heatmaps: Highlight areas of your app that are prone to slowdowns.
- Before-and-After Comparisons: Show how optimizations have improved animation smoothness.
Conclusion
Optimizing your app’s performance is an ongoing process, and run-time performance — how well your app handles animations and interactions — can make or break the user experience. By leveraging the PerformanceObserver API, you can easily track down and fix performance issues related to long animation frames. With detailed data at your fingertips, you can continually optimize your app, ensuring a smooth, responsive experience for all users.
Start tracking long animation frames today using the PerformanceObserver API and see how it can help you identify and fix the most stubborn performance issues in your app.
To explore more about the PerformanceObserver API and how it can help you optimize your web app, visit the MDN documentation.