On Github khan4019 / HighPerfJS
<input ng-model="name">input with ng-model becomes angular directive Setup a keydown listener to input
{{name}}
Something in angular named: $interpolation
interpolation: markup with expression => string
interpolation setup a $watch in current scope
$watch will be notified if "name" changes
$compile vs $interpolate vs $parse
var a = new Array(); //[] var b = new Array(3);//[undefined, undefined, undefined] var c = new Array(5, 6, 7);// [5, 6, 7]constructor is a function call plus execution, argument check
[]; //literals var d = [5, 6, 7];why [] faster, literal vs new
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);
b.indexOf(669, 500);
b.lastIndexOf(169, 500);test: fromIndex
var devs = [{ name: 'addy osmani', age: 29}, {name: 'paul irish', age:31}, {name: 'js dude', age: 13}];
devs.filter(function (el, i, arr) {
return el.age > 21;
});
function filterByForLoop(myArr, num){
var i = 0, j = 0, len, output=[];
for (i = 0, len = myArr.length; i < len; i ++) {
if (myArr[i].age > 21) {
output[j++]= myArr[i];
}
}
return output;
}
filter vs simple loop
var a = 2;
function b(){
console.log(a);
}
function foo(){
var d = 21;
console.log(d);
}
foo(); //21
console.log(d); //ReferenceError: d is not definedJS scope chain
var myArr = [0, 1, 2,3 4, 5 ...];
function useGlobal() {
for (var j = 0; j < 10000; j++){
console.log(myArr[j]);
}
}
function useLocal() {
var localArr = myArr;
for (var k = 0; k < 10000; k++){
console.log(localArr[k]);
}
}
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 list = e.target.parentNode;
list.parentNode.removeChild(list);
e.preventDefault();
});
js perf delegate
$(document).ready(function () {
console.log('document is ready. I can sleep now');
});
document.addEventListener('DOMContentLoaded', function(){
console.log('document is ready. I can sleep now');
});
document.onreadystatechange = function () {
if (document.readyState == "complete") {
console.log('document is ready. I can sleep now');
}
}
$('#myId');
$('.myClass');
$('div p');
document.querySelectorAll('#myId');
document.querySelectorAll('.myClass');
document.querySelectorAll('div p');
var nodes = document.querySelectorAll('.myClass');
//convert nodeList to array
var nodesArray = [].slice.call(nodes);
$(el).addClass('hide');
$(el).removeClass('hide');
$(el).toggleClass('hide');
$(el).hasClass('hide');
//IE9+
el.classList.add('hide');
el.classList.remove('hide');
el.classList.toggle('hide');
el.classList.contains('hide');
MDN: classList
$(el).hide(); $(el).show(); el.style.display = 'none'; el.style.display = '';
$(el).html(); $(el).html(setString); $(el).empty(); el.innerHTML; el.innerHTML = setString; el.innerHTML = '';
$(el).text(); el.textContent;you might not need jQuery
$(el).on(eventName, eventHandler);
$(el).on('click', function(e){
console.log(e.target);
});
//IE9+
el.addEventListener(eventName, eventHandler);
el.addEventListener('click', function(e){
console.log(e.target);
});
//remove event $(el).off(eventName, eventHandler); el.removeEventListener(eventName, eventHandler);
$(el).parent(); $(el).children(); $(el).next(); $(el).prev(); $(parent).append(el); $(el).remove();
el.parentNode; el.children; el.nextElementSibling; el.previousElementSibling parent.appendChild(el); el.parentNode.removeChild(el);
console.assert(myArray.length >5, "More than 5 elements");
function foo(){
console.count('fooed');
}
foo(); // fooed: 1
foo(); // fooed: 2
foo(); // fooed: 3
console.time("Array initialize");
//your code
console.timeEnd("Array initialize");
Using the console
Console API
\
/
Build you own rules, and update old ones.
Break All the Rules