cached jquery selector vs. javascript

JavaScript performance comparison

Test case created by Lars

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<div class="myElement">
 <span></span>
 <span></span>
 <span></span>
 <span></span>
 <span></span>
 <span></span>
</div>
    

Preparation code output

<div class="myElement"> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> </div>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
jQuery
$myElement = jQuery('.myElement span');
$myElement.each(function() {
  $myElement.find(this).css('color', 'green');
});
pending…
JavaScript
myElement = document.querySelector('.myElement');
myElement = myElement.querySelectorAll('span');
for (var i = 0, len = myElement.length; i < len; i++) {
  myElement[i].style.color = 'green';
}
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 Comments