aungmyooo2k17
How to Optimize CPU Usage in Live View Application when introducing Scrolling Animation
I have a live view application that displays exchange rates data. Within the application, there is a box called “announcement” which shows scheduled or adhoc messages. Sometimes the message consists of around 3 lines, but occasionally it can be longer. However, the message box can only display up to 3 lines. So, when the message exceeds 3 lines, I decided to implement a scrolling animation behavior.
Here’s how I implemented it:
First, I added a Phoenix Hook to listen for when the message box is loaded. If the message box is loaded, I assume that there is a message to display.
Hooks.MessageBox = {
mounted() {
updateAnimationForMessage("message", this);
},
updated() {
updateAnimationForMessage("message", this);
},
};
Then, I added a JavaScript function to check the number of lines in the current message.
const messageList = document.querySelector("#message-list");
const content = messageList.querySelector("li:first-child");
const cssCompute = window.getComputedStyle(content);
context.pushEvent(event, {
number_of_lines: Math.floor(
parseInt(cssCompute.height) / parseInt(cssCompute.lineHeight)
),
});
}
In the LiveView, I created a list with 10 rows of <li>Message here</li> elements using a for loop. These messages are scrolled up using CSS animation set to run infinitely. I push the number of lines count to the LiveView. The LiveView receives the number of lines and adds the CSS animation with inline styling.
["animation: message-slideup #{calculate_message_animation_time(number_of_lines) * 10}s linear infinite"]
I expected this code to work without any unexpected behavior or increased CPU usage. However, when I deployed it to production, I noticed that the CPU usage doubled with 200 clients accessing the application. Therefore, I am seeking help to resolve this issue.
Most Liked
thomas.fortes
What is the frequency of updateAnimationForMessage? If it’s running something like requestAnimationFrame and you’re pushing an event from there the event is hitting the server at least 60 times a second (more in high refresh rate monitors), which for 200 users would be 12000 events a second that the server has to handle, which would explain the memory use.
I agree with @ibarch, you probably should figure out a way to decouple the animation from the roundtrips to the server.
If it is only running a single time the problem isn’t in the javascript and we would need more info about your problem to figure out where the bottleneck is.
ibarch
Try to apply phx-update="ignore" attribute to the parent container and fill it with dummy values on LW mount.
If the scrolling runs smoothly, then it’s likely that Morphdom patching is interfering with your manual DOM updates in the hook. To resolve this issue you can try to configure Morpdom to carry over some properties during patching:
const liveSocket = new LiveSocket("/live", Socket, {
...,
dom: {
onBeforeElUpdated(fromEl, toEl) {
if (fromEl.matches(".parent")) {
// I'm not sure which properties need to be moved from fromEl to toEl
}
}
}
});
If animation jittering remains intact you need to debug your JS implementation.








