• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
21克
   首页    新随笔       管理    订阅  订阅

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

posted @ 2010-11-23 23:37  21克木子  阅读(261)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3