getElementById VS jQuery('#id') (v113)

Revision 113 of this benchmark created by Oleksandr Kelepko on


Preparation HTML

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

<script>
var MAX = 1000;
var elements = {};
for(var i = 0; i < MAX; ++i){
  var $el = $('<p id="foo' + i + '">Test</p>').appendTo($(document.body));
  elements['foo' + i] = $el[0];
}

var c = 0;
var next = function(){
  c++;
  if(c === MAX) c = 0;
  return 'foo' + c;
}
</script>

Setup

c = 0;

Test runner

Ready to run.

Testing in
TestOps/sec
getElById
document.getElementById(next());
ready
jQuery
$('#' + next());
ready
jQuery with getElById
$(document.getElementById(next()));
ready
getElById -
document.getElementById('missing'+next())
ready
jQuery -
$('#missing'+next())
ready
indexed
elements[next()]
ready

Revisions

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