I'm Alex, nice to meet you!
@alexnmoldovan
Co-Founder @JSHeroes
✍ freecodecamp.org/news/author/alexnm/
No Servus 🇷🇴Timișoara!
Open Source Engineer @teleportHQ
JavaScript
Style Calc
Layout
Paint
Composite
16 ms
16 ms
16 ms
16 ms
10ms
10ms
10ms
10ms
16 ms
16 ms
16 ms
16 ms
10ms
40ms
for (let index = 0; index < newsItems.length; index++) {
const newsBox = document.createElement('div')
/* ... */
const newsItemsList = document.getElementById('news-items-list')
newsItemsList.appendChild(newsBox)
}
const header = document.getElementById('header')
function handleScroll() {
if (window.scrollY > 50) {
header.style.position = "fixed"
}
}
Forced Synchronous Layout = FSL
const header = document.getElementById('header')
function handleScroll() {
if (window.scrollY > 50) {
header.style.position = "fixed"
}
}
addEventListener('touchstart', onTouchStart, {passive: true});
function animateBox() {
/* ... */
offset = offset + step
box.style.marginLeft = offset + "px";
/* ... */
requestAnimationFrame(animateBox);
}
Style
Layout
Paint
Composite
Style
Layout
Paint
Composite
Style
Layout
Paint
Composite
Layout
Paint
Composite
Paint
Composite
Composite
Does it change the geometry of the element?
(ex: width, margin, border)
Is it a visual property?
(ex: color, background-color, visibility)
Does it promote the element to a new layer?
(ex: transform, opacity)
Use it as a last resort
Don't over use it, composition is not cheap
Test how the browser optimizes the layers before using it
Avoid unnecessary or repetitive DOM access
Use requestAnimationFrame for all visual delays / animations
Throttle sensitive event handlers (ex: scroll, resize)
Use passive event listeners for an easy win
Avoid forced reflows / layout trashing
Prefer transform and opacity for css animations
Use will-change as a last resort to promote an element to its own layer
I'm open for questions!
@alexnmoldovan
Co-Founder @JSHeroes
✍ freecodecamp.org/news/author/alexnm/
Mulțam fain 🇷🇴Timișoara!
Open Source Engineer @teleportHQ
Ciao ciao!