TJ VanToll
$( "p" ).addClass( "foo" );
document.querySelectorAll( "p" ).classList.add( "foo" );
document.querySelectorAll( "p" )
.forEach(function() {
this.classList.add( "foo" );
});
var paragraphs = document.querySelectorAll( "p" );
for ( var i = 0; i < paragraphs.length; i++ ) {
paragraphs[ i ].classList.add( "foo" );
};
function find( selector ) {
return Array.prototype.slice.call(
document.querySelectorAll( selector )
);
}
find( "p" ).forEach(function( p ) {
p.classList.add( "foo" );
});
<div>
<p>Hello World</p>
</div>
<script>
var div = document.querySelector( "div" );
console.log( div.firstChild );
</script>
<div>
<p>Hello World</p>
</div>
<script>
var div = document.querySelector( "div" );
console.log( div.firstElementChild );
</script>
<div>
<p>Hello World</p>
</div>
<script>
var div = document.querySelector( "div" );
console.log( div.querySelectorAll( "> p" ) );
</script>
http://jsfiddle.net/tj_vantoll/Nt7Vq/
Result: Uncaught SyntaxError: Failed to execute query: '> p' is not a valid selector.
>
+
~
<div>
<p>Hello World</p>
</div>
<script>
var div = document.querySelector( "div" );
console.log( div.querySelectorAll( "div p" ) );
</script>
http://jsfiddle.net/tj_vantoll/8mw87/
Result: NodeList[1] (<p>)
<script src="jquery.js"></script>
<!-- before -->
<script src="jquery.js"></script>
<script src="app.js"></script>
<!-- after -->
<script src="app.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
Three RTTs!
Text --> Bytes --> AST --> JIT Compilation -> Execution
<script>var start = new Date();</script>
<script>
/* jQuery's minified source code in its entirety */
</script>
<script>alert( new Date() - start );</script>
Raw data of 5 loads in each browser. Times are in ms.
Browser | jQuery 1.10.2 | jQuery 2.0.3
------------------------|--------------------------|-------------------------
IE 11 | 18, 20, 20, 20, 24 | 18, 21, 14, 16, 15
Chrome 31 | 20, 8, 6, 5, 7 | 15, 8, 5, 7, 6
Safari 7 | 11, 4, 4, 4, 4 | 9, 5, 3, 3, 2
Firefox 26 | 12, 13, 13, 12, 13 | 12, 12, 11, 12, 12
iOS7 Safari | 60, 39, 58, 56, 58 | 40, 32, 37, 69, 40
Android 2.2 | 1080, 266, 434, 271, 470 | 928, 264, 494, 315, 220
Android 4.0 | 531, 141, 153, 112, 105 | 453, 106, 104, 145, 148
Chrome 31 (Android 4.4) | 271, 126, 101, 68, 53 | 219, 86, 38, 86, 123
Moore's law is the observation that, over the history of computing hardware, the number of transistors on integrated circuits doubles approximately every two years.http://en.wikipedia.org/wiki/Moore's_law
...and you're still seeing performance issues
Remember, jQuery is an amazing library that makes all of our lives easier. But you should always choose to use native DOM methods if they are available to you.http://www.leebrimelow.com/native-methods-jquery/
Premature optimization is the root of all evil.-Donald Knuth
I tried using the browser’s native innerHtml and getElementByClassName API methods instead of jQuery’s html and append. I thought native APIs might be easier for the browser to optimize and what I read confirmed that. But for whatever reason, it didn’t make much of a difference for Trello.http://blog.fogcreek.com/we-spent-a-week-making-trello-boards-load-extremely-fast-heres-how-we-did-it/
// Easily-parseable/retrievable ID or TAG or CLASS selectors
var rquickExpr = /^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/;
// Shortcuts
if ( (match = rquickExpr.exec( selector )) ) {
// Speed-up: Sizzle("#ID")
if ( (m = match[1]) ) {
...
}
// Speed-up: Sizzle("TAG")
} else if ( match[2] ) {
...
}
// Speed-up: Sizzle(".CLASS")
} else if ( (m = match[3]) && support.getElementsByClassName && context.getElementsByClassName ) {
...
}
}
jQuery doesn't force you to use its APIs. If you detect a slow code path, switch to native methods.
$( "div" ).each(function() {
this.classList.add( "red" );
});
$( "div" ).on( "click", function() {
this.classList.add( "red" );
});
write less, do more.