jQuery is not a seperate language it's a framework built on top of javascript
Released in 2006 by John Resig based on a bunch of helpers he regularly used.
He deveoped coz existing libs were not good enough or modified js itself
It became a instant hit and is now used by over 80% of internet
Its being developed by some of the brightest minds and supported by some great companies
Developed in the Wild Wild West of the browser land
over jQuery
and that too in a simple way
// '.classes', '[attributes]', '#ids'
document.querySelector(elem);
document.querySelectorAll(elems);
// '.classes', '[attributes]', '#ids'
$(elem);
jQuery
JavaScript
Code
85% - source
93% - source
// JavaScript uses a DOMTokenList to contain
// the class names, returned via the classList Object
elem.classList.add('fowd');
elem.classList.remove('fowd');
elem.classList.toggle('fowd');
//You can use below method to a jQuery Object
$(elem).addClass('fowd');
$(elem).removeClass('fowd');
$(elem).toggle('fowd');
jQuery
JavaScript
Code
85% - source
82% - source
el.className = el.className.replace(new RegExp('(^|\\b)' +
className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
el.className += ' ' + className;
Add Class
Remove Class
Alt Code
99%
elem.setAttribute('data-jquery', 'test'); // set
elem.getAttribute('data-jquery'); // get
$(elem).attr('data-jquery', 'test'); // set
$(elem).attr('data-jquery'); // get
jQuery
JavaScript
Code
86% - source
99%
if (el.addEventListener) {
el.addEventListener(eventName, someFunction);
} else { //IE 8-
el.attachEvent('on' + eventName, function(){
someFunction.call(el);
});
}
$(elem).on('click', someFunction);
jQuery
JavaScript
Code
94% - source
99% - source
document.addEventListener('DOMContentLoaded', Function_Name);
$(document).ready(Function_Name);
jQuery
JavaScript
Code
Performance Stats have, by now, become a joke
89% - source
// extend the Style Object, pure write, no loops
var elem = document.querySelector(elem);
elem.style.background = '#000';
elem.style.color = '#fff';
// creates an Object, will loop internally
$(elem).css({
"background" : "#000",
"color" : "#fff"
});
jQuery
JavaScript
Code
99%
Code - nodes
var paragraphs = document.querySelectorAll( "p" );
for (var i = 0; i < paragraphs.length; i++ ) {
paragraphs[ i ].classList.add('red');
};
jQuery('p').each(function(index, value) {
jQuery(this).addClass('red');
});
//or jQuery('p').addClass('red')
jQuery
JavaScript
var arr = [2, 3, 5, 7];
arr.forEach(function (value, index) {
//works ie9+ or use normal for loops
});
var arr = [2, 3, 5, 7];
$.each(arr, function(index, value) {
});
jQuery
JavaScript
Code - array
var obj = { a:'1', b:'2' };
for (var prop in obj) {
}
var obj = { a:'1', b:'2' };
$.each(obj, function(index, value) {
});
jQuery
JavaScript
Code - objects
The slow parsing of scripts on mobile browsers like Android forces us to use small JavaScript libraries.
but remember to pick the right one
"The simplest (and safest) classes of cross-browser fixes are those that exhibit two important traits:
/*
* Optional (non-Sizzle) selector module for custom builds.
*
* Note that this DOES NOT SUPPORT many documented jQuery
* features in exchange for its smaller size:
*
* Attribute not equal selector
* Positional selectors (:first; :eq(n); :odd; etc.)
* Type selectors (:input; :checkbox; :button; etc.)
* State-based selectors (:animated; :visible; :hidden; etc.)
* :has(selector)
* :not(complex selector)
* custom selectors via Sizzle extensions
* Leading combinators (e.g., $collection.find("> *"))
* Reliable functionality on XML fragments
* Requiring all parts of a selector to match elements under context
* (e.g., $div.find("div > *") now matches children of $div)
* Matching against non-elements
* Reliable sorting of disconnected nodes
* querySelectorAll bug fixes (e.g., unreliable :focus on WebKit)
*
* If any of these are unacceptable tradeoffs, either use Sizzle or
* customize this stub for the project's specific needs.
*/
Browser | Count |
---|---|
IE (all) | 50 |
IE 10, 11 | 9 |
Firefox | 10 |
Webkit | 20 |
Android | 7 |
IOS |
2 |
Chrome |
2 |
Opera |
6 |
Safari |
2 |
- Etherpad
80% of the end-user response time is spent on the front-end downloading all the components in the page: images, css, scripts etc. Reducing the number of components in turn reduces the number of HTTP requests required to render the page. This is the key to faster pages.
- YSlow
ajax.googleapis.com or code.jquery.com
" 81.9% of the websites use none of the JavaScript content delivery networks that we monitor.
Google Hosted Libraries is used by 15.8% of all the websites, that is a JavaScript content delivery network market share of 87.3%.
"
- w3techs
miss leads to 3 RTTs!
Native methods will almost always be faster, but in the vast majority of cases it doesn't matter.
"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."
- Fogcreek
// 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 ) {
...
}
}
But does it lead to faster site
" we found that lighter file size did not translate into better performance overall. Sure, Zepto downloaded quickly. But once loaded, it wasn't quite as fast as jQuery. It didn't help that many third-party plugins required jQuery, not Zepto — in fact, we found that some third party JavaScripts conflicted outright with Zepto."
var contentID = document.getElementById('contents'); //returns a HTML DOM Object
var contents = $(contentID); //returns a jQuery Object
var contents = $('#contents')[0]; //returns a HTML DOM Object
$('p').each(function() {
this.classList.add('red');
});
if something is slow use plain js. Mix and match for performance
in following cases
At the very least, make sure you know what jQuery is doing for you, and what it's not.
- Hubspot
To jQuery
or
Not To jQuery
hostname | percentage |
ajax.googleapis.com | 18.3% |
code.jquery.com | 1.4% |
yandex.st | 0.3% |
ajax.aspnetcdn.com | 0.2% |
What are the top 300K sites doing
jQuery Hosted from, 1 Mar 13
hostname | percentage |
1.4.2 (http) | 1.7% |
1.7.2 (http) | 1.6% |
1.7.1 (http) | 1.6% |
1.3.2 (http) | 1.2% |
1.7.2 (https) | 1.1% |
1.8.3 (http) | 1.0% |
What are the top 300K sites doing
jQuery Version if loading from Google, 1 Mar 13
var paragraphs = document.querySelectorAll( "p" );
var plen = paragraphs.length;
var i = 0;
for (i = 0; i < plen; i++ ) {
paragraphs[ i ].classList.add('red');
};
jQuery('p').addClass('red');
jQuery
JavaScript