var a = [];
for (var i = 0; i<10000; i++){
a.push(i);
}
var b = [];
for (var i = 0; i<10000; i++){
b[i] = i;
}
test: push vs assign
var a = [1, 2];
a.push(3, 4, 5);
a; // [1, 2, 3, 4, 5]
var b = [1, 2, 3, 4, 5, ...];
b.indexOf(669);
a[i]=5
is faster a.push(5)
var a = 2;
function foo(){
var b = 21;
}
var a = 2;
function foo(){
var b = 21;
return a + b;
}
var a = 99;
function useGlobal() {
for (j = 0; j < 1000000; j++){
console.log(a);
}
}
var a = 99;
function useLocal() {
var localA = a;
for (var k = 0; k < 1000000; k++){
console.log(localA);
}
}
local vs global variable
<ul id="listToDestroy">
<li><a href="#">first item</a></li>
<li><a href="#">second item</a></li>
<li><a href="#">third item</a></li>
<li><a href="#">forth item</a></li>
<li><a href="#">Fifth item</a></li>
</ul>
var el = document.getElementById('listToDestroy');
el.addEventListener('click', function (e) {
var elm = e.target.parentNode;
elm.parentNode.removeChild(elm);
e.preventDefault();
});
js perf delegate
a[i]=5
is faster a.push(5)