check elements selected vs no check

Benchmark created by Gary Green on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><input class="blah" value="blah"/><div></div><div></div><div></div>
<div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><input class="blah" value="blah"/><div></div><input class="blah" value="blah2"/><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<script>
  $.fn.exists = function(func) {
   if (this.length) func.call(this);
  };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
no check
$('.blah').addClass('success').filter(function() {
 return $(this).val() == 'blah';
}).hide().animate({
 left: 500
}).find('*');
ready
plugin exists check
$('.blah').exists(function() {

 $(this).addClass('success').filter(function() {
  return $(this).val() == 'blah';
 }).hide().animate({
  left: 500
 }).find('*');

});
ready
inline length check
var $ele =
$('.blah');
if ($ele) {
 $ele.addClass('success').filter(function() {
  return $(this).val() == 'blah';
 }).hide().animate({
  left: 500
 }).find('*');
}
ready

Revisions

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

  • Revision 1: published by Gary Green on