Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of jQuery Selectors with ID

I know in jQuery if we use ID to select elements,it's so efficient.I have a question about this selectors:

please consider this 3 selectors:

$('#MyElement')

$('#Mytbl #MyElement')

$('#Mytbl .MyClass')

which one is faster and why?How I can check the time elapsed to select my element in jQuery?

like image 643
Arian Avatar asked Nov 22 '12 08:11

Arian


4 Answers

A direct ID selector will always be the fastest.

I've created a simple test case based on your question...

http://jsperf.com/selector-test-id-id-id-id-class

Selecting nested ID's is just wrong, because if an ID is unique (which it should be), then it doesn't matter if it's nested or not.

like image 140
ahren Avatar answered Nov 18 '22 19:11

ahren


this is the way to stop times between some javascript calls

selectorTimes = [];
var start = new Date().getTime();
$('#MyElement')
selectorTimes.push(new Date().getTime()-start);

start = new Date().getTime()
$('#Mytbl #MyElement')
selectorTimes.push(new Date().getTime()-start);

start = new Date().getTime()
$('#Mytbl .MyClass')
selectorTimes.push(new Date().getTime()-start);

console.log(selectorTimes);

i think the second selector is not efficient, if you have a domid select this directly: $('#MyElement')

like image 27
silly Avatar answered Nov 18 '22 20:11

silly


The first one is the fastest, simply because it only has 1 property to look for. However,

document.getElementById("MyElement")

is even faster, though. It's native javascript, and unlike jQuery, the browser immediately knows what you want to do, instead of having to run through a load of jQuery code to figure out what it is you're looking for, in the first place.

You can use jsPerf to run a speed test, to compare the functions: Test Case. Results:

$('#MyElement')
Ops/sec: 967,509
92% slower

$('#Mytbl #MyElement')
Ops/sec: 83,837
99% slower

$('#Mytbl .MyClass')
Ops/sec: 49,413
100% slower

document.getElementById("MyElement")
Ops/sec: 10,857,632
fastest

Like expected, the native getter is the fastest, followed by the jQuery getter with only 1 selector at less than 10% of the native speed. The jQuery getters with 2 parameters don't even get close to the operations per second of native code, especially the class selector, since classes are usually applied to multiple elements, compared to ID's. (Native ID selectors stop searching after they've found one element, I'm not sure if jQuery does, too.)

like image 5
Cerbrus Avatar answered Nov 18 '22 18:11

Cerbrus


A couple things:

  • More selectors = slower search. If you can get your desired result with fewer predicates, do so.
  • Getting an element by ID is quicker than getting by a class. getElementById is a core function of JavaScript that is heavily optimised because it is used so frequently. Leverage this when possible.
  • The space selector (' ') is much more costly than the child selector ('>'). If you can use the child selector, do so.

These rules apply to CSS as they do JavaScript and jQuery.

like image 3
Levi Botelho Avatar answered Nov 18 '22 20:11

Levi Botelho