High Performance Javascript

原PPT链接:

http://www.slideshare.net/nzakas/high-performance-javascript-webdirections-usa-2010

NicholasC.Zakas《Javascript高级程序设计》的作者

 

摘要如下:

 

1. The browser UI thread isresponsible for both UI updates and Javascript execution.

 Only one can happen at a time.



2. Long-running Javascript = Unresponsive UI

    How long is too long? 

         100ms. Recommendation:50ms

    Long UI updates =Unresponsive UI

 

 

3. Ways to ensure Javascript doesn’t run away:

  A. Timers

  B. Web Worker

    a. Asynchronous Javascript execution

    b. Execution happens outside of UI thread

      a) Not on the UI thread = no UI delays

    d. Data-driven API

      a) Data is serialized when sending data into or out of Worker

      b) No access to DOM, BOM

      c) Completely separate execution environment

 

 

4. Repaint and Reflow

 

A repaint occurs when a visual change doesn’t require recalculation of layout

Changes to visibility, colors (text / background), background image,etc

e.g. 

 

A reflow occurs when a visual change requires a change in layout

Initial page load, browser resize, DOM structure change, layout style change, layout information retrieved.

e.g. 

 

Repaints and reflows are queued up as Javascript executes and thenexecuted in order.

Limiting repaints / reflows improves overall performance.

 

Solutions:

A. Perform DOM manipulations off-document

  a) Remove element from the document, make changes, insert back into document.  

  b) Set element’s display to “none”, make changes, set display back to default

  c) Build up DOM changes on a DocumentFragment then applay all atonce.

 

  DocumentFragment:

 

B. Group Style Changes

 

C. Avoid Accidental Reflow

  a) Minimize access to layout information

    offsetTop,offsetLeft, offsetWidth, offsetHeight

    scrollTop, scrollLeft,scrollWidth, scrollHeight

    clientTop, clientLeft, clientWidth, clientHeight

    Most computed styles

  b) If a value is used more than once, store in local variable

转载于:https://www.cnblogs.com/21gram/archive/2010/11/23/1886023.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值