Javascript performance
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.
- with/catch
- local variables
- 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:
- store out of scope varialbes in local variables, espeically global variables
- avoid With statement
- be careful with try/catch clause
- use sparsly closure.
- 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:
- Create execution context and destory
- 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的更多相关文章
- nosql db and javascript performance
http://blog.csdn.net/yiqijinbu/article/details/9053467 http://blog.nosqlfan.com/tags/javascript http ...
- how to optimize javascript performance
https://developers.google.com/speed/articles/optimizing-javascript http://developer.yahoo.com/perfor ...
- how to measure function performance in javascript
how to measure function performance in javascript Performance API Performance Timeline API Navigatio ...
- 我所知道的Javascript
javascript到了今天,已经不再是我10多年前所认识的小脚本了.最近我也开始用javascript编写复杂的应用,所以觉得有必要将自己的javascript知识梳理一下.同大家一起分享javas ...
- 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 ...
- 转:45 Useful JavaScript Tips, Tricks and Best Practices
原文来自于:http://flippinawesome.org/2013/12/23/45-useful-javascript-tips-tricks-and-best-practices/ 1 – ...
- [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 ...
- JavaScript周报#184
This week’s JavaScript news Read this issue on the Web | Issue Archive JavaScript Weekly Issue 184Ju ...
- Performance js
转贴:https://10up.github.io/Engineering-Best-Practices/javascript/#performance Performance Writing per ...
随机推荐
- [原创] zabbix学习之旅二:yum安装
对于允许连接公网的环境下,显然通过yum安装是最为简单方便的,也是官网推荐的安装方式.通过这种方式安装,会将php.apache.zabbix本身都一并安装,解决了烦人的依赖包问题. 本文将介绍如 ...
- block extends include三者的差别跟用法
block extends include三者的差别跟用法 一.定义基础模板,在html内容中定义多个block块,block由子模板引用同名block块,来决定是否替换这些部分{% block ti ...
- 关于Web服务器域名设置相关知识积累
1.第一个问题,如何将一个服务器映射到一个域名上呢? 在申请域名的时候,会配置服务器IP和域名的对应关系,所以如果系统中只有一个应用的情况下,应用服务器不需要做任何配置. 2.在Tomcat服务 ...
- 【CodeForces】【311E】Biologist
网络流/最大权闭合图 题目:http://codeforces.com/problemset/problem/311/E 嗯这是最大权闭合图中很棒的一道题了- 能够1A真是开心-也是我A掉的第一道E题 ...
- JSP页面批量选择&全选操作&选择回显
效果如下: js验证部分: 页面body部分: 附:控制器Controller中验证批量选择条件回显:
- TLS学习总结
我们有知道 Immunity Debugger,OD 调试器,在调试程序时会设断在OEP(修改第一个字节0xcc).我在想,使用什么编程技术,代码可以在OEP前被执行.在网上找了些资料,在论坛上看到许 ...
- p3p之讲解
P3P是一种被称为个人隐私安全平台项目(the Platform for Privacy Preferences)的标准,能够保护在线隐私权,使Internet冲浪者可以选择在浏览网页时,是否被第三方 ...
- HDU-2604_Queuing
题目:Problem Description Queues and Priority Queues are data structures which are known to most comput ...
- 用fabric部署维护kle日志收集系统
最近搞了一个logstash kafka elasticsearch kibana 整合部署的日志收集系统.部署参考lagstash + elasticsearch + kibana 3 + kafk ...
- 怎样把网站js文件合并成一个?几种方法可以实现
我们在建网站时经常会用js特效代码以使页面更美观,比如js幻灯片代码.js下拉菜单等,但是网页特效一多,如果js文件没有合并的话会降低网站的性能,这时我们就要考虑合并js文件了,ytkah总结了以下几 ...