`
haouziwefe
  • 浏览: 127567 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
社区版块
存档分类
最新评论

Speed up your JavaScrip 4[]

    博客分类:
  • java
阅读更多
    <div class="blog_content">在web开发中,javascript的一个很重要的作用就是对dom进行操作,可你知道么?对dom的操作是非常昂贵的,因为这会导致浏览器执行回流操作,而执行了过多的回流操作,你就会发现自己的网站变得越来越慢了,我们应该尽可能的减少dom操作。本文是这个系列的最后一篇,给出了一些指导性原则,比如在什么时候应该对dom可以进行什么样的操作等。<br><br>【原文】<a rel="external" href="http://www.nczonline.net/about/" target="_blank"><span style="color: #108ac6;">nicholas c. zakas</span></a> - <a rel="external" href="http://www.nczonline.net/blog/2009/02/03/speed-up-your-javascript-part-4/" target="_blank"><span style="color: #108ac6;">speed up your javascript, part 4</span></a><br>【译文】<a rel="external" href="http://cuimingda.com/" target="_blank"><span style="color: #108ac6;">明达</span></a> - <a rel="external" href="http://cuimingda.com/2009/02/speed-up-your-javascript-part-4.html" target="_blank"><span style="color: #108ac6;">如何提升javascript的运行速度(dom篇)</span></a><br><br>以下是对原文的翻译:<br><br>在过去的几周中,我为大家介绍了几种可以加快javascript脚本运行速度的技术。第一节 介绍了如何优化循环。第二节的重点放在优化函数内部代码上,还介绍了队列(queuing)和记忆化(memoization)两种技术,来减轻函数的工作负担。第三节就如何将递归转换为迭代循环或者记忆化方式的话题,展开了讨论。第四节是这个系列的最后一篇,也就是本文,将重点阐述过多的dom操作所带来的影响。<br><br>我们都知道,dom操作的效率是很低的,而且不是一般的慢,而且这也是引发性能问题的常见问题之一。为什么会慢呢?因为对dom的修改为影响网页的用户界面,重绘页面是一项昂贵的操作。太多的dom操作会导致一系列的重绘操作,为了确保执行结果的准确性,所有的修改操作是按顺序同步执行的。我们称这个过程叫做回流(reflow),同时这也是最昂贵的浏览器操作之一。回流操作主要会发生在几种情况下:<br><br>* 当对dom节点执行新增或者删除操作时。 <br>* 动态设置一个样式时(比如element.style.width="10px")。 <br>* 当获取一个必须经过计算的尺寸值时,比如访问offsetwidth、clientheight或者其他需要经过计算的css值(在兼容dom的浏览器中,可以通过getcomputedstyle函数获取;在ie中,可以通过currentstyle属性获取)。<br><br>解决问题的关键,就是限制通过dom操作所引发回流的次数。大部分浏览器都不会在javascript的执行过程中更新dom。相应的,这些浏览器将对对 dom的操作放进一个队列,并在javascript脚本执行完毕以后按顺序一次执行完毕。也就是说,在javascript执行的过程中,用户不能和浏览器进行互动,直到一个回流操作被执行。( 失控脚本对话框会触发回流操作,因为他执行了一个中止javascript执行的操作,此时会对用户界面进行更新)<br><br>如果要减少由于dom修改带来的回流操作,有两个基本的方法。第一个就是在对当前dom进行操作之前,尽可能多的做一些准备工作。一个经典的例子就是向document对象中添加很多dom节点:
<div class="ubbpanel codepanel"><div class="ubbcontent"><div class="dp-highlighter"><br>
0
0
分享到:
评论

相关推荐

    Beginning JavaScript, 4th Edition

    to-date JavaScript programming techniquesContinuing in the superlative tradition of the first three editions, Beginning JavaScript, Fourth Edition, gets you up to speed on all the new advances in ...

    Mastering.JavaScript.High.Performance.1784397296

    Next, you will move on to learn about DOM optimization, JavaScript promises, and web workers to better break up your large codebase. You will also learn about JavaScript performance on mobile ...

    如何提升JavaScript的运行速度(函数篇).doc

    Zakas 于 2009 年 1 月 20 日在个人网站上发表的《Speed up your JavaScript, Part 2》。本文主要讨论了如何重构嵌套循环、递归,以及那些在函数内部同时执行很多子操作的函数,以提高 JavaScript 的运行速度。 ...

    [JavaScript] JavaScript 从入门到精通 (英文版)

    Speed up and simplify app development with jQuery Quickly retrieve data from a server using AJAX requests Adapt your app for mobile devices with jQuery Mobile Build Windows 8 apps using HTML, CSS, and...

    Beginning JavaScript with DOM Scripting and Ajax: Second Editon

    This completely updated second edition covers everything you need to know to get up-to-speed with JavaScript development and add dynamic enhancements to web pages, right from the basics. As well as ...

    Beginning JavaScript with DOM Scripting and Ajax (pdf + ePub)

    This completely updated second edition covers everything you need to know to get up-to-speed with JavaScript development and add dynamic enhancements to web pages, right from the basics. As well as ...

    Web性能优化系列 10个提升JavaScript性能的技巧

    当谈到 JS 性能的时候,Zakas差不多就是你要找的,2010年六月他在Google Tech Talk发表了名为《Speed Up Your Javascript》的演讲。 但 Javascript 性能优化绝不是一种书面的技术,Nicholas 的技术演进列出了10条...

    JavaScript 圣经第5版-Javascript编程宝典--黄金版 .rar

    This book will bring programmers and non-technical professionals, including casual programmers and scripters, painlessly up to speed on all aspects of mastering JavaScript. Key topics include ...

    JavaScript Programmers Reference

    who are just learning the language and want to come up to speed quickly. In either case we assume you have at least a basic background in programming. Chapter 1, in particular, assumes you are coming...

    JavaScript 提升运行速度之循环篇 译文

    原文标题:Speed up your JavaScript, Part 1 原文作者:Nicholas C. Zakas 在我 上一篇帖子 (译文 ) 中,谈到了各个浏览器究竟会在什么情况下弹出脚本失控提示,对于Internet Explorer 来说,当浏览器执行了数量...

    PhoneGap: Beginner’s Guide

    You will find plenty of fully explained code and ample screenshots in the book to ease and speed up your understanding. This book is for developers, ideally with web development experience, who are ...

    PhoneGap Beginners Guide Sep 2011

    You will find plenty of fully explained code and ample screenshots in the book to ease and speed up your understanding. This book is for developers, ideally with web development experience, who are ...

    ReactJS.Blueprints.1785886541

    Learn how to speed up your development process and save valuable time Work though step-by-step tutorials that provide easy-to-understand solutions to real-world problems Book Description The ...

    React.Native.Blueprints.epub

    Use external modules to speed up the development and maintenance of your projects Explore the different UI and code patterns to be used for iOS and Android Understand the best practices which can be ...

Global site tag (gtag.js) - Google Analytics