I just went through some vedio related to javascript performance which is great, Here is the notes I made:

  • Scope management

1. Identifier Resolution

Every time the function is executed, the execution context is created. The scope chain in the execution context stores the objects to be resolved in order.

sequence in the scope chain.

    1. with/catch
    2. local variables
    3. global variables

According to the scope chain, we can see the deeper you go the scope chain, the longer it take to resolve the identifers.

Recomendation:

    1. store out of scope varialbes in local variables, espeically global variables
    2. avoid With statement
    3. be careful with try/catch clause
    4. use sparsly closure.
    5. don't forget var when declaring variables
  • Data access

  Accessing data from literal variable and local variable is fartest.

Array item, object property look up takes more time.

property depth, the deeper the property, the longer it takes to retrieve. for example, object.name < object.name.name

property notation

object.name and object["name"] no difference generally. safari. dot notation is faster.

Recommendation

Store them in local variables, if the following happens:

1. if any object property accessed more than once.

2. any array item accessed more than once.

3. minimize deep object property/array item look up

example:

function process (data){

if(data.count>0) {

 for (var i=0;i<data.count;i++){

processdata(data.item[i]);

}

}

}

after change made:

function process (data){

var count = data.count;

     item = data.item;

if(count>0){

for(var i=0;i<count;i++)

  processdata(item[i]);

}
  • Loops

What does matter?

Amount of the work done per iteration, including the terminal condition evaluation incrementing/decrementing, here is the example:

for(var i=0;i<values.length;i++)
{
process (values[i]);
}

Recommendation:

  • Eliminate the object propery/array item lookups
  • Combine control condition and control variable change - work avoidance
var len = values.length

for (var i=len; i--;){

process(values[i]);

}
  • Avoid foreach statement which calls another function, here is the example:
values.foreach( function (data){

  process(data);

})

Reasons:

  1. Create execution context and destory
  2. The new execution context has its own scope chain.

Resolution:

8x peformance boost if we go with the regular loop like while, for, etc.

  • DOM

1. HTMLCollection Objects.

document.images,

document.getElementsByTagName

They are automatcially updated when the uderlying document is changed.

var divs = document.getElementByTagName("div");

for(var i=0;i<div.length;i++){

   var newdiv = document.createElement("div");

   document.body.appendChild(newdiv);

}

What the result if we run the script above:

infinite loop!! it's a infinite loop.

  • HtmlCollection element look like arrays, but are not bracket notation, length property
  • it represents the result of a specific query
  • the query is re-run each time the object is accessed.
  • including acessing lenth and specific items
  • much slower than accessing the same on the arrays (exceptions: Opera, Safira). here is the example:
var items = [{},{},{},{},{},{},{}];

for( var i=0;i<items.length;i++){

}

var divs = document.getElementByTagName("div");

for(var i=0;i<divs.length;i++){

}
the performance difference: firefox:15X; chrome: 53X; IE:68X

After change made: no much difference.

for( var i=0;len=divs.length;i<len;i++){

}

for(var i=0;i<divs.length;i++){

}

Recommendaton:

  • minimize accessing to the property of a object. store length, items in local variables if used frequently.
  • if you need to access items in order frequently, copy into a regular array.

Reflow

When reflow happen

  • Initial page load
  • Browser window resize
  • Layout style applied
  • Add/remove dom node
  • Layout information retrieved

How to avoid reflow:

  • DocumentFragment

It's a dcoument-like object, consider a child of the document from which it was created, not visually represented, when you pass documentFragement to appendChild(), appends all of its children rather than itself.

var list = document.getElementById("list");

var fragment = document.createDocumentFragment();

for(var i=0;i<10;i++){

 var item = document.createElement("li");

 item.innerHTML =  "Option #" + (i+1);

 fragment.appenChild(item); --No reflow

}

list.appenChild(fragment); --reflow

Recommendation:

  • Minimize the changes on style property (element.style.height = "100PX")
  • define CSS class with all changes and just change the className property

Layout information retrieved

all the statements below causes reflow:

var width = element.offsetwidth;

var scrollleft = element.scrollleft;

var display = window.getComputedStyle(div, '');

Recommendation on Speed up Dom:

  • Be careful using HTMLCollection objects
  • Perform DOM manipulation off the document
  • Change CSS classes, not CSS styles
  • Be careful when accessing layout information

Javascript performance的更多相关文章

  1. nosql db and javascript performance

    http://blog.csdn.net/yiqijinbu/article/details/9053467 http://blog.nosqlfan.com/tags/javascript http ...

  2. how to optimize javascript performance

    https://developers.google.com/speed/articles/optimizing-javascript http://developer.yahoo.com/perfor ...

  3. how to measure function performance in javascript

    how to measure function performance in javascript Performance API Performance Timeline API Navigatio ...

  4. 我所知道的Javascript

    javascript到了今天,已经不再是我10多年前所认识的小脚本了.最近我也开始用javascript编写复杂的应用,所以觉得有必要将自己的javascript知识梳理一下.同大家一起分享javas ...

  5. 45 Useful JavaScript Tips, Tricks and Best Practices(有用的JavaScript技巧,技巧和最佳实践)

    As you know, JavaScript is the number one programming language in the world, the language of the web ...

  6. 转:45 Useful JavaScript Tips, Tricks and Best Practices

    原文来自于:http://flippinawesome.org/2013/12/23/45-useful-javascript-tips-tricks-and-best-practices/ 1 – ...

  7. [Webpack] Analyze a Production JavaScript Bundle with webpack-bundle-analyzer

    Bundle size has a huge impact on JavaScript performance. It's not just about download speed, but all ...

  8. JavaScript周报#184

    This week’s JavaScript news Read this issue on the Web | Issue Archive JavaScript Weekly Issue 184Ju ...

  9. Performance js

    转贴:https://10up.github.io/Engineering-Best-Practices/javascript/#performance Performance Writing per ...

随机推荐

  1. 关于Google+以及Facebook第三方登录实现的一点总结

    简述 最近项目中有关于第三方登陆的需求,第三方Facebook以及Google +登录. 正好这几天把这个需求做得差不多了,收个尾,作为一个这方面之前基本从未涉及的小白,总结下开发流程以及过程中遇到的 ...

  2. HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. matrix_last_acm_2

    2014年广州站网络赛 北大命题 password 123 B http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97257#problem/ ...

  4. [百度空间] ld: add library file reference by path & file name

    By default, -l option will search libraries with lib* prefix in speficied search paths. i.e. 1 ld -o ...

  5. Codeforces Round #259 (Div. 2)-D. Little Pony and Harmony Chest

    题目范围给的很小,所以有状压的方向. 我们是构造出一个数列,且数列中每两个数的最大公约数为1; 给的A[I]<=30,这是一个突破点. 可以发现B[I]中的数不会很大,要不然就不满足,所以B[I ...

  6. YARN应用程序的开发步骤

    开发基于YARN的应用程序需要开发客户端程序和AppMaster程序: 我们基于程序自带的例子来实现提交application 到YARN的ResourceManger. Distributed Sh ...

  7. ObjC的Block中使用weakSelf/strongSelf @weakify/@strongify

    首先要说说什么时候使用weakSelf和strongSelf. 下面引用一篇博客<到底什么时候才需要在ObjC的Block中使用weakSelf/strongSelf>的内容: Objec ...

  8. C# 对动态编辑的一些学习笔记

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Comp ...

  9. Ogre1.8.1源码编译

    本文的编译环境为Windows7_SP1 + VS2010_SP1 + CMake2.8.11   :) 资源下载 1. 下载Ogre1.8.1的源代码,下载链接地址:http://www.ogre3 ...

  10. Nginx SPDY Pagespeed模块编译——加速网站载入

    在看<Web性能权威指南>的时候,看到了SPDY这货,于是便开始折腾起了这个了,也顺便把pagespeed加了进去. Nginx SPDY 引自百科~~ SPDY(读作“SPeeDY”)是 ...