remove attribute vs set to false (v2)

Revision 2 of this benchmark created by Spencer Drager on


Description

For cases in which you want an attribute that works like a flag.

Preparation HTML

<input id="input" type="text" value="The quick brown fox jumps over the lazy dog" disabled="true">

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var input = document.getElementById('input'),
      JQinput = $(input);
  test = function(i) {
   if (i.getAttribute('disabled') == true) {
    throw 'This test should fail:' + i.getAttribute('disabled');
   }
  };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
removeAttribute()
input.removeAttribute('disabled');
test(input);
ready
setAttribute()
input.setAttribute('disabled', false);
test(input);
ready
disabled = false
input.disabled = false;
test(input);
ready
jQuery removeAttr requiring $()
$(input).removeAttr('disabled');
test(input);
ready
jQuery attr requiring $()
$(input).attr('disabled', '');
test(input);
ready
jQuery removeAttr pre-$()'d
JQinput.removeAttr('disabled');
test(input);
ready
jQuery attr pre-$()'d
JQinput.attr('disabled', '');
test(input);
ready
disabled = false on a de-$()'d object
JQinput[0].disabled = false;
test(input);
ready

Revisions

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

  • Revision 1: published by Dan Manastireanu on
  • Revision 2: published by Spencer Drager on
  • Revision 3: published by Tim Tucker on
  • Revision 4: published by Tim Tucker on
  • Revision 5: published by Tim Tucker on