Improve Javascript Performance by Reducing DOM Access and Reflow
1. Improve your
Javascript
Performance
Devon2011 DAUM
Wednesday, November 23, 11
2. About Me
Web Programmer 2002 ~ 2009
Frontend Programmer 2009 ~
FT Projects
Cafe
Tistory T-Edition
Daum Editor
Frontend Research / Support
Wednesday, November 23, 11
20. function initUI() {
var body = document.body,
links = document.getElementsByTagName(a),
i = 0,
len = links.length;
while (i len) {
update(links[i++]);
}
document.getElementById(go-btn).onclick = function() {
start();
}
bd.className = active;
}
Wednesday, November 23, 11
21. function initUI() {
var doc = document,
body = doc.body,
links = doc.getElementsByTagName(a),
i = 0,
len = links.length;
while (i len) {
update(links[i++]);
}
doc.getElementById(go-btn).onclick = function() {
start();
}
bd.className = active;
}
Wednesday, November 23, 11
22. function initUI() {
var doc = document,
body = doc.body,
links = doc.getElementsByTagName(a),
i = 0,
len = links.length;
while (i len) {
update(links[i++]);
}
doc.getElementById(go-btn).onclick = function() {
start();
}
bd.className = active;
}
Wednesday, November 23, 11
35. DOM
Reflow
UI Thread
Wednesday, November 23, 11
36. Daum Top DOM visualization - http://www.aharef.info/static/htmlgraph/
Wednesday, November 23, 11
37. DOM
Daum Top DOM visualization - http://www.aharef.info/static/htmlgraph/
Wednesday, November 23, 11
38. ‘The Document Object Model (DOM) is an
application programming interface (API) for
HTML and XML documents’
Wednesday, November 23, 11
39. ‘The Document Object Model (DOM) is an
application programming interface (API) for
HTML and XML documents’
Wednesday, November 23, 11
40. DOM is not part of
javascript engine
Javascript Engine DOM Engine
JScript (jscript.dll) Trident (mshtml.dll)
Nitro WebCore
V8 WebCore
TraceMonkey Gecko
Wednesday, November 23, 11
50. Parsing HTML to
Render tree Layout of the Painting the
construct the
construction render tree render tree
DOM tree
Wednesday, November 23, 11
51. Parsing HTML to
Render tree Layout of the Painting the
construct the
construction render tree render tree
DOM tree
Wednesday, November 23, 11
52. Parsing HTML to
Render tree Layout of the Painting the
construct the
construction render tree render tree
DOM tree
{ width, height, x, y }
{ width, height, x, y }
{ width, height, x, y }
{ width, height, x, y }
{ width, height, x, y }
{ width, height, x, y }
Wednesday, November 23, 11
53. Parsing HTML to
Render tree Layout of the Painting the
construct the
construction render tree render tree
DOM tree
Wednesday, November 23, 11
55. Causing Reflow
Page renders initially
Window is resized
Visible DOM elements are added or removed
Element change position
Element change size
margin, padding, border, width, height
text changes, image replaced with different size
Wednesday, November 23, 11
56. Reflow is Expensive
Batch Style Changes
Use Flow-Off Technique
Avoid Geometry Access
Wednesday, November 23, 11
73. “Javascript that executes in
whole seconds is probably
doing something wrong...”
- Brendan Eich, Creator of Javascript
One Second
Wednesday, November 23, 11
74. How Long Is Too Long?
“0.1 second is about the limit for having the
user feel that the system is reacting
instantaneously, meaning that no special
feedback is necessary except to display the
result.”
- Jakob Nielsen, 1968
Wednesday, November 23, 11