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. Python使用SMTP发送邮件[HTML格式、送带附件]

    SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. python的smtplib提供了一 ...

  2. Hibernate中启用日志

    Problem How do you determine what SQL query is being executed by Hibernate? How can you see the Hibe ...

  3. 【长期兼职】每天3小时写作=每月4000元外快(IT兼职写手)

    只要你有经验,每周平均有20来个小时的兼职时间. 只要你愿意静静地写一些心得总结. 那么就可以联系我QQ164349714,敲门:写作. 地址不限.特长不限.学历不限.年龄不限. 主要写作方向:1.投 ...

  4. 【环境】VS2013和MATLAB相互调用混合编程

    Visual Studio和MATLAB混合编程,有两种方法: 1 MATLAB调用C程序: 2 VS调用MATLAB(目前见到的都是VS,其他编译器如codeblocks,或不提供这项功能): 前一 ...

  5. css3 的content 属性

    content属性想必大家都熟悉了,一般结合伪类一起使用,表示显示的内容 例如:.box:before{content:"hello";width:100px;line-heigh ...

  6. 3527: [Zjoi2014]力 - BZOJ

    题目大意:给出n个数qi,定义 Fj为        令 Ei=Fi/qi,求Ei. 看了很久题解,终于有些眉目,因为知道要用FFT,所以思路就很直了 其实我们就是要±1/(j-i)^2 ( j-i大 ...

  7. 【BZOJ】【3894】文理分科

    网络流/最小割 rausen大爷太神辣-作为一个蒟蒻还是搬运题解吧…… 很明显的一道网络流题.. 首先把所有值的加起来,再减掉网络流最小割值就好了,问题就是如何建图.这貌似也是考了好多次了的... 把 ...

  8. Codeforce 438D-The Child and Sequence 分类: Brush Mode 2014-10-06 20:20 102人阅读 评论(0) 收藏

    D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...

  9. mysql查看连接数和状态,设置连接数和超时时间

    1.mysql> show status like '%connect%'; Connections,试图连接到(不管是否成功)MySQL服务器的连接数.   Max_used_connecti ...

  10. Connection reset by peer: socket write error 连数据库出现改错

    1.网络原因 2.从池中获取连接后没有释放到池中导致的