{Carrer web log} ←
About Repaint and Reflow in JavaScript
Tuesday, February 08, 2011 { 10 Comments }
Recently I stumbled at this Google Documentation - REST from JavaScript realizing how little web developers are aware about JavaScript performance. I heard about Repaint and Reflow mainly from the work of Stoyan Stefanov and Nicholas Zakas, so I’m writing this short article to help raise the awareness about Repaint and Reflow and JavaScript performance in general.So what is Reflow?
A picture is worth a thousand words and probably the video is worth a thousand pictures :)
Here is the video:
Reflow is the process by which the geometry of the layout engine’s formatting object are computed – Chris Waterson, Mozilla
"Repaint - also known as redraw - is what happens whenever something is made visible when it was not previously visible, or vice versa, without altering the layout of the document. An example would be when adding an outline to an element, changing the background color, or changing the visibility style. Repaint is expensive in terms of performance, as it requires the engine to search through all elements to determine what is visible, and what should be displayed." – Opera
Basically Reflow is layout change and Repaint color change.
So you get the picture Repaint and Reflow are highly expensive operation for the browser. So you need to minimize the number of the Repaints and Reflows when you do DOM manipulations.
Let’s take the example that I mentioned previously:
So every time we use insert HTML into the DOM structure we probably triggers reflow. Maybe this isn’t the best example that shows pure resizing of the layout, but we can avoid accessing the DOM multi times.
// before
function hndlr(response) {
for (var i = 0; i < response.items.length; i++) {
var item = response.items[i];
// in production code, item.htmlTitle should have the HTML entities escaped.
document.getElementById("content").innerHTML += "<br>" + item.htmlTitle;
}
}
// after – calling innerHTML just once
function hndlr(response) {
var html = " ";
var y = response.items.length;
for (var i = 0; i < y; i++) {
var item = response.items[i];
html += "<br>" + item.htmlTitle;
}
// making just one Reflow
document.getElementById("content").innerHTML = html
}
Here are more case studies and other examples:
Rendering: repaint, reflow/relayout, restyle
The new game show: "Will it reflow?"
Writing Efficient JavaScript
When does JavaScript trigger reflows and rendering?
Repaint and reflow
10 Responses to “About Repaint and Reflow in JavaScript”
- // Unknown // 2/08/2011
- // Vladimir // 2/08/2011
- // pomeh // 2/09/2011
-
// pomeh
// 2/09/2011
Not only you call innerHTML once, but you call document.getElementById once too ! Minimizing the number of DOM request and caching those request is also a very good thing to do. For me, it is more important than the innerHTML stuff, but I'm curious to see what performance tests says about that :)
Also, about IE crossing "the bridge" as you said, you may like this little article http://www.gnegg.ch/2010/10/how-to-kill-ie-performance/
It just kill me too ! - // Vladimir // 2/10/2011
- // Website Designers // 2/21/2011
- // Javascript Countdown Timer // 3/12/2011
- // Brett Widmann // 4/24/2011
- // Abhi // 10/05/2012
- // Vladimir // 10/05/2012
<< Home
About Me <<<
Name: Vladimir Carrer
vladocar [at] gmail.com
Location: Verona, Italy
I'm a web designer, developer, teacher, speaker, generally web addicted ...
My projects <<<
- AI Prompt Directory
- Hand Drawn Icons
- Font Design Inspiration
- Font Pairings
- Free SVG Cut File
- Upcoming NFT projects
- Discord Tutorials
- Free Sublimation Designs
- Tech Feed
- MySQL Lite Administrator
- Quark Mini PHP CMS
- Formy - CSS Form Framework
- Emastic - CSS Framework
- Malo - CSS Library
- The Golden Grid
- 1 line CSS Grid Framework
- Two Lines CSS Framework
- Child Selector System - CSS Framework
- Better Web Readability Project
- Azbuka - CSS Typographical Base Rendering Library
- ClipR - bookmarklet for better reading
- CSS3 Action Framework
- CSS Mini Reset
- HTML5 Mini Template
- CSS Mobile Reset
- picoCSS - JavaScript Framework
- HTML Lorem Ipsum Crash Test
- Object Auto Documentation - JavaScript
- o - JS Library for Object Manipulation
- Foxy - CSS Framework
- Tumblr Free Theme - Better Readability Project
- Box - CSS Framework
- SMART CSS GRID
- nanoJS - Minimal JS DOM Library
- Flexy CSS Framework
- Katana is CSS Layout System made with Flexbox
- Micro CSS Reset
- 60 Grid System
- Simple CSS Button
- ramd.js JavaScript library for making web applications.
- Minimal Notes web app build with Vue.js
- Scribble Font for Prototyping & Wireframing
- Flex One - 1 CSS Class System
- Floaty - CSS Float Based Layout System
- Infinity CSS Grid
- CLI Convert websites into readable PDFs
- keywords-extract - CLI tool, extract keywords from any web page.
- screenshoteer - Make website screenshots and mobile emulations from the command line.
- Basic.css - Classless CSS Starter File
§§Previous Posts <<<
- Better Readability Project - Free Tumblr Theme
- CSS3 Chat Bubbles
- Line length and Font Size relation in design
- Safari - open links in new tabs
- Hacker News Mobile (Front Page Reader )
- CSS Mobile Reset
- About JSONP in JavaScript
- Two Lines CSS Framework
- About the dollar sign ($) in JavaScript
- Canvas Grid - JS1K entry
Other Profiles <<<
Content is licensed under a Creative Commons Public Domain License