Introduction
User interfaces (UI) dictate how users interact and navigate through your application. An application with a heavy load time or janky animations can negatively affect user experience, causing users to abandon your application entirely. It’s essential to understand how to optimize UI performance to create a smooth, engaging feel for your users.
In this tutorial, we’ll explore techniques to fine-tune UI performances such as lazy loading, caching resources, using requestAnimationFrame for UI updates, and virtualizing lists for large datasets.
Accelerating Load Times with Lazy Loading
Lazy loading is a strategy in which only necessary or critical user interface components are loaded up front. Anything that isn’t immediately needed is deferred until it becomes necessary.
JavaScript’s dynamic import() function allows for module loading on demand. Consider this example which only requests a module when a specific event occurs:
document.getElementById('button').addEventListener('click', () => {
import('./module.js')
.then(module => {
module.function();
});
});
React further optimizes this using the React.lazy()
function, which automatically manages bundle-splitting. This practice can dramatically improve site speed and user experience.
Caching Resources: Storing for Performance
Caching is a technique where you store frequently used data points to speed up subsequent data retrievals. It differs from lazy loading in that cached items aren’t deferred, but rather retrieved ahead of time to prevent slow load times later on.
There are several methods to cache data in modern web development, but for this article, let’s focus on localStorage
and Indexed Database API.
With localStorage
, you can store key-value pairs in a web browser with a high level of simplicity. It’s widely supported and easy to use:
localStorage.setItem('key', 'value');
const value = localStorage.getItem('key');
IndexedDB provides a robust indexed data storage solution that can store significant amounts of structured data. However, it has a much steeper learning curve than localStorage
. Here is a simple usage example:
let db;
let request = indexedDB.open('MyTestDatabase');
request.onerror = function(event) {
alert("Why didn't you allow my web app to use IndexedDB?!");
};
request.onsuccess = function(event) {
db = event.target.result;
};
Remember that both localStorage
and IndexedDB are subject to the same-origin policy. Also, you should always try to store only the minimal amount of data necessary as both have storage limitations.
Using requestAnimationFrame for UI Updates
The requestAnimationFrame
method provides a native API for refreshing the UI, telling the browser that you wish to perform an animation and requesting the browser to call a specified function to update animation before the next repaint.
Utilizing requestAnimationFrame
can make animations smoother and reduce unnecessary computations. Here’s a simple example of an animation loop:
function animate() {
// update objects
// draw
requestAnimationFrame(animate);
}
animate();
It is key to ensure animations run at 60 frames per second (fps) for a smooth experience. Adding too many animations or failing to optimize them can cause jank, making animations appear choppy. As Paul Lewis suggests, “stick to compositor-only properties and manage layer count” to optimize animations.
Boosting Performance with List Virtualization
For large datasets, each additional DOM element decreases performance. Virtualizing these lists meets the challenge head-on by only rendering what is necessary. JavaScript libraries such as react-window and react-virtualized can be used to implement this.
Simplified example using react-window:
import { FixedSizeList as List } from "react-window";
const Row = ({ index, style }) => (
<div style={style}>Row {index}</div>
);
const MyComponent = () => (
<List
height={150}
itemCount={1000}
itemSize={35}
width={300}
>
{Row}
</List>
);
Conclusion
Optimizing your UI’s performance isn’t about implementing every best practice. It’s about understanding your app, how it interacts with the browser, and what your problem areas are. Just remember, as Addy Osmani points out, “the cost of n
millisecond delays can quickly add up”.
By leveraging lazy loading, caching, requestAnimationFrame
, and list virtualization, you’re already on the path of delivering a performant, responsive user experience. So go forth, and optimize!
- Mohseen