element selectors are the fastest This is because they’re backed by native DOM operations (eg. getElementById()). by Sameera Thilakasiri | www.sameerast.com | Twitter @ sameerast 3
due to no native calls available that we can take advantage of. querySelector() and querySelectorAll() help with this in modern browsers. 5 by Sameera Thilakasiri | www.sameerast.com | Twitter @ sameerast
translated to $.parent.find(‘child’).show(); causing it to be slower. ~5-10% slower than the fastest option 7 by Sameera Thilakasiri | www.sameerast.com | Twitter @ sameerast
find nodes following other nodes in the same tree. ~50% slower than the fastest option 9 by Sameera Thilakasiri | www.sameerast.com | Twitter @ sameerast
combinatory selector, however Sizzle works from right to left. Bad as it will match .child before checking it’s a direct child of the parent. ~70% slower than the fastest option 10 by Sameera Thilakasiri | www.sameerast.com | Twitter @ sameerast
is constrained by the same rules as 4). Internally also has to translate to using .find() ~77% slower than the fastest option 11 by Sameera Thilakasiri | www.sameerast.com | Twitter @ sameerast
cached here, so it doesn’t need to be refetched from the DOM. Without caching this is ~ 16% slower. Directly uses native getElementById, getElementsByName, getElementsByTagName to search inside the passed context under the hood. 13 by Sameera Thilakasiri | www.sameerast.com | Twitter @ sameerast
children = $(‘.parents’).find(‘.child’), //bad kids = parents.find(‘.child’); //good Caching just means we’re storing the result of a selection for later re-use. 15 by Sameera Thilakasiri | www.sameerast.com | Twitter @ sameerast
DOM and return a new collection You can then do anything with the cached collection. Caching will decrease repeat selections. 16 by Sameera Thilakasiri | www.sameerast.com | Twitter @ sameerast
a jQuery object and support chaining. This means after executing a method on a selection, you can continue executing more. Less code and it’s easier to write! 17 by Sameera Thilakasiri | www.sameerast.com | Twitter @ sameerast
memory and using a single .append() instead. Multiple appends can be up to 90% slower when not appending to cached selectors and up to 20% slower with them. 19 by Sameera Thilakasiri | www.sameerast.com | Twitter @ sameerast
a node Allows you to re-insert the node to the DOM once you’re ready Up to 60% faster than working with undetached nodes. 20 by Sameera Thilakasiri | www.sameerast.com | Twitter @ sameerast
this is actually much faster.. $(‘#elem’).data( key , value ); $.data(‘#elem’, key , value); as there’s overhead creating a jQuery object and doing data-parsing in the first. 21 by Sameera Thilakasiri | www.sameerast.com | Twitter @ sameerast
Specialist in UI, UX, SEO, IA, IxD, RWD. He is blogging technical areas and lifestyle photographer while is leisure. He can be reached by http://www.sameerast.com or @sameerast 23 by Sameera Thilakasiri | www.sameerast.com | Twitter @ sameerast