As most web developers are aware, a significant amount of a script's running time may be spent performing DOM operations triggered by the script rather than executing the JS byte code itself. One such potentially costly operation is layout (aka reflow) -- the process of constructing a render tree from a DOM tree. The larger and more complex the DOM, the more expensive this operation may be.

An important technique for keeping a page snappy is to batch methods that manipulate the DOM separately from those that query the state. For example:

// Suboptimal, causes layout twice.
var newWidth = aDiv.offsetWidth + 10; // Read
aDiv.style.width = newWidth + 'px'; // Write
var newHeight = aDiv.offsetHeight + 10; // Read
aDiv.style.height = newHeight + 'px'; // Write

// Better, only one layout.
var newWidth = aDiv.offsetWidth + 10; // Read
var newHeight = aDiv.offsetHeight + 10; // Read
aDiv.style.width = newWidth + 'px'; // Write
aDiv.style.height = newHeight + 'px'; // Write

Stoyan Stefanov's tome on repaint, relayout and restyle provides an excellent explanation of the topic.

This often leaves developers asking the question: What triggers layout? Last week Dimitri Glazkov answered this question with this codesearch link. Trying to understand it better myself, I went through and translated it into a list of properties and methods. Here they are:

Element

clientHeight, clientLeft, clientTop, clientWidth, focus(), getBoundingClientRect(), getClientRects(), innerText, offsetHeight, offsetLeft, offsetParent, offsetTop, offsetWidth, outerText, scrollByLines(), scrollByPages(), scrollHeight, scrollIntoView(), scrollIntoViewIfNeeded(), scrollLeft, scrollTop, scrollWidth

Frame, Image

height, width

Range

getBoundingClientRect(), getClientRects()

SVGLocatable

computeCTM(), getBBox()

SVGTextContent

getCharNumAtPosition(), getComputedTextLength(), getEndPositionOfChar(), getExtentOfChar(), getNumberOfChars(), getRotationOfChar(), getStartPositionOfChar(), getSubStringLength(), selectSubString()

SVGUse

instanceRoot

window

getComputedStyle(), scrollBy(), scrollTo(), scrollX, scrollY, webkitConvertPointFromNodeToPage(), webkitConvertPointFromPageToNode()

This list is almost certainly not complete, but it is a good start. The best way to check for over-layout is to watch for the purple layout bars in the Timeline panel of Chrome or Safari's Inspector.

来源:http://gent.ilcore.com/2011/03/how-not-to-trigger-layout-in-webkit.html

最新文章

  1. C#编译执行过程
  2. Linux时间戳和标准时间的互转
  3. 关于AutoCAD 2014的securityload…
  4. postgres与osm初步使用
  5. Notepad++配置Python开发环境
  6. 【原】UIView实现点击着重效果的解决方案
  7. CCF 201403-1 相反数 (水题)
  8. iOS系统控件显示中文
  9. UI5_HomeWork
  10. DashClock
  11. ActionBarSherlock学习笔记 第一篇——部署
  12. 局部刷新Ajax
  13. MySQL中的EXPLAIN
  14. Java用户界面技术
  15. D3_book 11.2 stack
  16. 【文文殿下】[AH2017/HNOI2017]礼物
  17. [Baltic 2011]Lamp BZOJ2346
  18. input 输入框只能输入纯数字
  19. 软工实践团队项目-"智能聊天机器人"简介
  20. mysql 随机获取一条或多条数据

热门文章

  1. vs2010取消显示所有文件 崩溃
  2. mysql 删除时候有外键提示问题解决
  3. asp值mysql驱动
  4. js中arguments的用法
  5. html(一)
  6. css+div解决文字溢出控制显示字数
  7. 伪共享和缓存行填充,从Java 6, Java 7 到Java 8
  8. HTML5 canvas绘图
  9. Spark HA实战
  10. 双十一 VS 火车票(12306)