.length vs .is(':checked')

Benchmark created by praveen on


Preparation HTML

<input class="s" type="checkbox" />
  <input class="s" type="checkbox" />
<input class="s" type="checkbox" />
  <input class="s" type="checkbox" />
<input class="s" type="checkbox" />
  <input class="s" type="checkbox" />
<input class="s" type="checkbox" />
  <input class="s" type="checkbox" />
  <input class="s" type="checkbox" checked/>
  <input class="s" type="checkbox" checked/>
  <input class="s" type="checkbox" />
  <input class="s" type="checkbox" checked/>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Test runner

Ready to run.

Testing in
TestOps/sec
using length
if ($('.s:checked').length === 0) {
  alert('You have to have at least one course selected');
  return false;
}
ready
using is(':checked')
var tot;
$('.s').each(function() {
  if ($(this).is(':checked')) {
    tot = tot + 1;
  }
});

if (tot == 12) {
  alert('You have to have at least one course selected');
  return false;
}
ready

Revisions

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

  • Revision 1: published by praveen on