Important performance optimization distal js

1 minimize the use of global find, such as a global variable, if you want to use repeatedly, you can save it as a global variable and then use local variables
eg:function(){
var body=document.body;
alert(body):
body.innerHTML="hello world"
}
2. optimization cycle (1) Impairment cycle i - (2) do-while loop for faster than (3) Cycling conditions such simplified calculation loop condition less
 
3 Dom layer - to minimize on-site operation,
That is, for example, you can create new lot dom dom full of debris for recycling and then added in one go faster than one would want to add dom
var list=document.getElementById("mylist");
var frament=document.createDocumentFragment();
for (var i=0;i<10;i++){
var item=document.creatElement('li')
frament.appendChild(item)
}
list.appendChild(frament)
4 Dom layer - using less HTMLCollection
For example, the cycle of acquiring length dom
var images=document.getElementsByTagName('img')
for(i=0,len=images.length;i<len;i++){
 
}
So do not always access the images of HTMLCollention
In some cases will visit HTMLCollention
(1) were getElementsByTagName () call
(2) obtain the childNodes attribute element
(3) to obtain the attributes attribute of the element
(4) set as a special access documet.forms, document.images

Guess you like

Origin www.cnblogs.com/bing1991/p/11275428.html