简介

延缓执行 JavaScript 是一个能有效提高网页加载速度以及提升用户阅读体验质量的途径。从实际经验来看,将我们的网站从经济实惠的 VPS 迁移到 Softlayer(美国著名的一个数据中心)独立服务器平台上只能给网站加载速度带来20%的提升,但是通过延缓执行 JavaScript 却能帮助提速 50% ,不妨看看 Google Webmaster 工具 > Site Performance(网站性能)的统计结果:

实战

网页开发遵循一个假设,就算有 JS 文件突然被中断了,只要没有 JS 执行错误,那网页就一定会被正确渲染。简单的说,延缓执行 JS 可以采取下面两种规则:

等到页面 Document 准备好之后再来执行内联的 JS 代码,这些代码至少也得放在页面底部。

动态地加载外部 JavaScript 文件。如果多个 JS 文件之间存在依赖,确保主要的 JS 文件引用写在网页底部以便最后加载。

下面这个主页面的代码片段指出了我们如何在开发中延缓 JavaScript 的执行。

  1. <script type="text/javascript">// <![CDATA[
  2. _lazyLoadScripts = new Array();
  3. _lazyExecutedCallbacks = new Array();
  4. // ]]></script>
  5. <script type="text/javascript" src="/scripts/jquery-1.4.4.min.js"></script>
  6. <script type="text/javascript" src="/scripts/website-lazy-load.js"></script>

在开发中经常会有些嵌套网页或者构件需要依赖一些外部 JS 文件或 JS 代码的执行。在这种情况下,可以像上面例子那样在主页面顶部定义两个变量 “_lazyLoadScripts” 和 “_lazyExecutedCallbacks” 。

在下面代码片段中,你可以看到这两个变量是如何被使用在嵌套网页或构件上的。

  1. <script type="text/javascript">// <![CDATA[
  2. _lazyExecutedCallbacks.push(function ()
  3. {
  4. // in the case you need to execute some scripts in a nested page or module.
  5. // don't execute them explicitly, but push them into the callback list.
  6. });
  7. // ]]></script>
  8. <script type="text/javascript">// <![CDATA[
  9. // push the external JS files into the list for deferring loading.
  10. _lazyLoadScripts.push("/scripts/your-script.js");
  11. // ]]></script>

这些被压入(push)到 “_lazyExecutedCallbacks” 里的 JS 代码和被插入到 “_lazyLoadScripts” 里的外部 JS 文件全部都会在 “website-lazy-load.js” 里被执行,执行的代码片段如下:

  1. // dynamically load external JS files when document ready
  2. // dynamically load external JS files when document ready
  3. function loadScriptsAfterDocumentReady()
  4. {
  5. if (_lazyLoadScripts && _lazyLoadScripts != null)
  6. {
  7. for (var i = 0; i < _lazyLoadScripts.length; i++)
  8. {
  9. var scriptTag = document.createElement('script');
  10. scriptTag.type = 'text/javascript';
  11. scriptTag.src = _lazyLoadScripts[i];
  12. var firstScriptTag = document.getElementsByTagName('script')[0];
  13. firstScriptTag.parentNode.insertBefore(scriptTag, firstScriptTag);
  14. }
  15. }
  16. }
  17. // Execute the callback when document ready.
  18. function invokeLazyExecutedCallbacks()
  19. {
  20. if (_lazyExecutedCallbacks && _lazyExecutedCallbacks.length > 0)
  21. for(var i=0; i<_lazyExecutedCallbacks.length; i++)
  22. _lazyExecutedCallbacks[i]();
  23. }
  24. // execute all deferring JS when document is ready by using jQuery.
  25. jQuery(document).ready(function ()
  26. {
  27. loadScriptsAfterDocumentReady();
  28. invokeLazyExecutedCallbacks();
  29. });

小贴士

开发网页的合理步骤应该是首先编写 HTML 和 CSS 。等这些网页在浏览器里能够正确地(符合你的期望)被渲染出来之后,再开始编写 JS 代码来支持动画或者其他的效果。

不要在 HTML 页面上的任何一个元素上编写 onclick="..." 代码来绑定事件,但是可以在 HTML Document 都准备好的情况下进行绑定。这样可以避免在 JS 文件加载完成之前因用户触发了 onclick 事件而导致的 JS 错误。

如果你的网站需要广泛地加载外部 JS 文件,那么将它们写在 “website-lazy-load.js” 里动态的加载进来,例如 Google Analytics tracking 的JS 文件、 Google AdSense 的JS 文件等等。

这种方法同样地对 CSS 文件也有效。但是别对 主CSS 文件这么做。

原文链接:http://my.oschina.net/u/563639/blog/59214

performance的更多相关文章

  1. Performance Monitor4:监控SQL Server的IO性能

    SQL Server的IO性能受到物理Disk的IO延迟和SQL Server内部执行的IO操作的影响.在监控Disk性能时,最主要的度量值(metric)是IO延迟,IO延迟是指从Applicati ...

  2. Performance Tuning

    本文译自Wikipedia的Performance tuning词条,原词条中的不少链接和扩展内容非常值得一读,翻译过程中暴露了个人工程学思想和英语水平的不足,翻译后的内容也失去很多准确性和丰富性,需 ...

  3. Performance Monitor3:监控SQL Server的内存压力

    SQL Server 使用的资源受到操作系统的调度,同时,SQL Server在内部实现了一套调度算法,用于管理从操作系统获取的资源,主要是对内存和CPU资源的调度.一个好的数据库系统,必定在内存中缓 ...

  4. [MySQL Reference Manual] 23 Performance Schema结构

    23 MySQL Performance Schema 23 MySQL Performance Schema 23.1 性能框架快速启动 23.2 性能框架配置 23.2.1 性能框架编译时配置 2 ...

  5. Unity性能优化(2)-官方教程Diagnosing performance problems using the Profiler window翻译

    本文是Unity官方教程,性能优化系列的第二篇<Diagnosing performance problems using the Profiler window>的简单翻译. 相关文章: ...

  6. 使用ANTS Performance Profiler&ANTS Memory Profiler工具分析IIS进程内存和CPU占用过高问题

    一.前言 最近一段时间,网站经常出现两个问题: 1.内存占用率一点点增高,直到将服务器内存占满. 2.访问某个页面时,页面响应过慢,CPU居高不下. 初步判断内存一点点增多可能是因为有未释放的资源一直 ...

  7. KPI:Key Performance Indicator

    通信中KPI,是Key Performance Indicators的缩写,意思是关键性能指标.performance 还有绩效:业绩的意思,但显然不适用于这种场合. 通信中KPI的内容有:掉话率.接 ...

  8. Performance Monitor1:开始性能监控

    Performance Monitor是Windows内置的一个可视化监控工具,能够在OS级别上实时记录系统资源的使用情况,通过收集和存储日志数据,在SQL Server发生异常时,能够还原系统当时的 ...

  9. Performance Monitor2:Peformance Counter

    Performance Counter 是量化系统状态或活动的一个数值,Windows Performance Monitor在一定时间间隔内(默认的取样间隔是15s)获取Performance Co ...

  10. Disk IO Performance

    一,使用 Performance counter 监控Disk IO问题 1,Physical Disk vs. Logical Disk Windows可以在一个Physical Disk上划出若干 ...

随机推荐

  1. Java批量处理数据

    要求:共1000条数据,第一次批量插入100条,第二次批量插入101到200条,依次插入数据: 实现方式这里选择了两种常用的方式,都是使用List操作: 第一种实现思路如下: <1> 原先 ...

  2. HDOJ 1197 Specialized Four-Digit Numbers

    Problem Description Find and list all four-digit numbers in decimal notation that have the property ...

  3. HDU5029--Relief grain (树链剖分+线段树 )

    题意:n个点构成的无根树,m次操作, 对于操作 x y z, 表示 x 到 y 路径上的 每个点 加一个 z 数字,可重复加.最后输出每个点 加的次数最多的那个数字,如果没有输出0. 赤裸裸的树链剖分 ...

  4. POJ(2784)Buy or Build

    Buy or Build Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1369   Accepted: 542 Descr ...

  5. php防sql注入、xss

    php自带的几个防止sql注入的函数http://www.php100.com/html/webkaifa/PHP/PHPyingyong/2013/0318/12234.html addslashe ...

  6. (转)iOS5:[UIDevice uniqueIdentifier]的替代方案

    背景: 大多数应用都会用到苹果设备的UDID号,UDID通常有以下两种用途: 1)用于一些统计与分析目的:[第三方统计工具如友盟,广告商如ADMOB等] 2)将UDID作为用户ID来唯一识别用户,省去 ...

  7. Ubuntu 12.04 搭建Android开发环境

    Ubuntu 12.04 搭建Android开发环境 2013/7/29 Linux环境下搭建Android开发环境 大部分开发人员可能都在Windows下做开发,可能是感觉在Windows下比较方便 ...

  8. 10个利用Eclipse调试Java的常见技巧

    http://www.open-open.com/news/view/1ad9099 阅读目录 1. Conditional Breakpoint 2. Exception Breakpoint 3. ...

  9. @IBDesignable和@IBInspectable

    近期一直在看苹果公司提供的两本swift官方教程电子书,一部是<The Swift Programming Language>,还有一部是<Using Swift With Coco ...

  10. Java 动态代理机制详解(JDK 和CGLIB,Javassist,ASM)

    class文件简介及加载 Java编译器编译好Java文件之后,产生.class 文件在磁盘中.这种class文件是二进制文件,内容是只有JVM虚拟机能够识别的机器码.JVM虚拟机读取字节码文件,取出 ...