query selector vs cache dom object vs jquery object

JavaScript performance comparison

Test case created by ssavajols

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id='element-id'></div>
      
<script>
Benchmark.prototype.setup = function() {
  var element = document.getElementById('element-id');
  var $element = $('#element-id');

};
</script>

Preparation code output

<div id='element-id'></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
querySelector
document.querySelector('#element-id').className = '';
document.querySelector('#element-id').className = 'this-is-classname';
pending…
cache var
element.className = '';
element.className = 'this-is-classname';
pending…
jQuery no cache
$('#element-id').attr('class', '');
$('#element-id').attr('class', 'this-is-classname');
pending…
jQuery cache
$element.attr('class', '');
$element.attr('class', 'this-is-classname');
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