performance
简介
延缓执行 JavaScript 是一个能有效提高网页加载速度以及提升用户阅读体验质量的途径。从实际经验来看,将我们的网站从经济实惠的 VPS 迁移到 Softlayer(美国著名的一个数据中心)独立服务器平台上只能给网站加载速度带来20%的提升,但是通过延缓执行 JavaScript 却能帮助提速 50% ,不妨看看 Google Webmaster 工具 > Site Performance(网站性能)的统计结果:
实战
网页开发遵循一个假设,就算有 JS 文件突然被中断了,只要没有 JS 执行错误,那网页就一定会被正确渲染。简单的说,延缓执行 JS 可以采取下面两种规则:
等到页面 Document 准备好之后再来执行内联的 JS 代码,这些代码至少也得放在页面底部。
动态地加载外部 JavaScript 文件。如果多个 JS 文件之间存在依赖,确保主要的 JS 文件引用写在网页底部以便最后加载。
下面这个主页面的代码片段指出了我们如何在开发中延缓 JavaScript 的执行。
- <script type="text/javascript">// <![CDATA[
- _lazyLoadScripts = new Array();
- _lazyExecutedCallbacks = new Array();
- // ]]></script>
- <script type="text/javascript" src="/scripts/jquery-1.4.4.min.js"></script>
- <script type="text/javascript" src="/scripts/website-lazy-load.js"></script>
在开发中经常会有些嵌套网页或者构件需要依赖一些外部 JS 文件或 JS 代码的执行。在这种情况下,可以像上面例子那样在主页面顶部定义两个变量 “_lazyLoadScripts” 和 “_lazyExecutedCallbacks” 。
在下面代码片段中,你可以看到这两个变量是如何被使用在嵌套网页或构件上的。
- <script type="text/javascript">// <![CDATA[
- _lazyExecutedCallbacks.push(function ()
- {
- // in the case you need to execute some scripts in a nested page or module.
- // don't execute them explicitly, but push them into the callback list.
- });
- // ]]></script>
- <script type="text/javascript">// <![CDATA[
- // push the external JS files into the list for deferring loading.
- _lazyLoadScripts.push("/scripts/your-script.js");
- // ]]></script>
这些被压入(push)到 “_lazyExecutedCallbacks” 里的 JS 代码和被插入到 “_lazyLoadScripts” 里的外部 JS 文件全部都会在 “website-lazy-load.js” 里被执行,执行的代码片段如下:
- // dynamically load external JS files when document ready
- // dynamically load external JS files when document ready
- function loadScriptsAfterDocumentReady()
- {
- if (_lazyLoadScripts && _lazyLoadScripts != null)
- {
- for (var i = 0; i < _lazyLoadScripts.length; i++)
- {
- var scriptTag = document.createElement('script');
- scriptTag.type = 'text/javascript';
- scriptTag.src = _lazyLoadScripts[i];
- var firstScriptTag = document.getElementsByTagName('script')[0];
- firstScriptTag.parentNode.insertBefore(scriptTag, firstScriptTag);
- }
- }
- }
- // Execute the callback when document ready.
- function invokeLazyExecutedCallbacks()
- {
- if (_lazyExecutedCallbacks && _lazyExecutedCallbacks.length > 0)
- for(var i=0; i<_lazyExecutedCallbacks.length; i++)
- _lazyExecutedCallbacks[i]();
- }
- // execute all deferring JS when document is ready by using jQuery.
- jQuery(document).ready(function ()
- {
- loadScriptsAfterDocumentReady();
- invokeLazyExecutedCallbacks();
- });
小贴士
开发网页的合理步骤应该是首先编写 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的更多相关文章
- Performance Monitor4:监控SQL Server的IO性能
SQL Server的IO性能受到物理Disk的IO延迟和SQL Server内部执行的IO操作的影响.在监控Disk性能时,最主要的度量值(metric)是IO延迟,IO延迟是指从Applicati ...
- Performance Tuning
本文译自Wikipedia的Performance tuning词条,原词条中的不少链接和扩展内容非常值得一读,翻译过程中暴露了个人工程学思想和英语水平的不足,翻译后的内容也失去很多准确性和丰富性,需 ...
- Performance Monitor3:监控SQL Server的内存压力
SQL Server 使用的资源受到操作系统的调度,同时,SQL Server在内部实现了一套调度算法,用于管理从操作系统获取的资源,主要是对内存和CPU资源的调度.一个好的数据库系统,必定在内存中缓 ...
- [MySQL Reference Manual] 23 Performance Schema结构
23 MySQL Performance Schema 23 MySQL Performance Schema 23.1 性能框架快速启动 23.2 性能框架配置 23.2.1 性能框架编译时配置 2 ...
- Unity性能优化(2)-官方教程Diagnosing performance problems using the Profiler window翻译
本文是Unity官方教程,性能优化系列的第二篇<Diagnosing performance problems using the Profiler window>的简单翻译. 相关文章: ...
- 使用ANTS Performance Profiler&ANTS Memory Profiler工具分析IIS进程内存和CPU占用过高问题
一.前言 最近一段时间,网站经常出现两个问题: 1.内存占用率一点点增高,直到将服务器内存占满. 2.访问某个页面时,页面响应过慢,CPU居高不下. 初步判断内存一点点增多可能是因为有未释放的资源一直 ...
- KPI:Key Performance Indicator
通信中KPI,是Key Performance Indicators的缩写,意思是关键性能指标.performance 还有绩效:业绩的意思,但显然不适用于这种场合. 通信中KPI的内容有:掉话率.接 ...
- Performance Monitor1:开始性能监控
Performance Monitor是Windows内置的一个可视化监控工具,能够在OS级别上实时记录系统资源的使用情况,通过收集和存储日志数据,在SQL Server发生异常时,能够还原系统当时的 ...
- Performance Monitor2:Peformance Counter
Performance Counter 是量化系统状态或活动的一个数值,Windows Performance Monitor在一定时间间隔内(默认的取样间隔是15s)获取Performance Co ...
- Disk IO Performance
一,使用 Performance counter 监控Disk IO问题 1,Physical Disk vs. Logical Disk Windows可以在一个Physical Disk上划出若干 ...
随机推荐
- 8.2.1.2 How MySQL Optimizes WHERE Clauses MySQL 优化WHERE 子句
8.2.1.2 How MySQL Optimizes WHERE Clauses MySQL 优化WHERE 子句 本节讨论优化用于处理WHERE子句, 例子是使用SELECT 语句,但是相同的优化 ...
- head,tail,cat,more,less
tail FILE -n 4,查看文件最后4行内容head FILE -n 10,查看文件最前4行内容 使用cat more less都可以查看文本内容,但是它们三者有什么区别呢?more和less的 ...
- Sum
Problem Description XXX is puzzled with the question below: 1, 2, 3, ..., n (1<=n<=400000) are ...
- Binary Tree Zigzag Level Order Traversal (LeetCode) 层序遍历二叉树
题目描述: Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474 My Submissions Given a binary tree, ...
- 【Ruby on Rails 学习一】ubuntu14.04配置rvm与ruby
要安装ruby,首先要安装rvm,借助rvm安装ruby rvm 的全称是 Ruby Version Manager ,是一款由 Wayne E. Seguin 开发的一款命令行工具.rvm 能够让 ...
- split
import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; /** * 解析知网文章的页面 ...
- RequireJS入门(三)
这篇来写一个具有依赖的事件模块event.event提供三个方法bind.unbind.trigger来管理DOM元素事件. event依赖于cache模块,cache模块类似于jQuery的$.da ...
- JS-Math内置对象
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 文件下载,带转码->pdf->swf
private String upload = "保存的路径"; //文件下载 public String download() { //初始化 this.initContext( ...
- 【ThinkingInC++】64、重载new和delete,来模仿内存的分配
/** * 书本:[ThinkingInC++] * 功能:重载new和delete.来模仿内存的分配 * 时间:2014年10月5日14:30:11 * 作者:cutter_point */ #in ...