remove attribute vs set to false (v3)

Revision 3 of this benchmark created by Tim Tucker on


Description

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

Preparation HTML

<input id="inputSame" type="text" value="The quick brown fox jumps over the lazy dog">
<input id="inputDifferent" type="text" value="The quick brown fox jumps over the lazy dog">
<script>
  var inputSame = document.getElementById("inputSame");
  var inputDifferent = document.getElementById("inputDifferent");
  var stringSame = "same";
  var stringDifferent = "different";

  var removeThenSet = function(input) {
      input.removeAttribute("aria-label");
      input.setAttribute("aria-label", stringSame);
      }
      
      
      
  var setOnly = function(input) {
      input.setAttribute("aria-label", stringSame);
      }
      
      
      
  var setIfDifferent = function(input, logSomething) {
      var value = input.getAttribute("aria-label");
      if (value !== stringSame) {
        input.setAttribute("aria-label", stringSame);
      }
      }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
remove then set, same
inputSame.setAttribute("aria-label", stringSame);
inputDifferent.setAttribute("aria-label", stringDifferent);
removeThenSet(inputSame);
ready
remove then set, different
inputSame.setAttribute("aria-label", stringSame);
inputDifferent.setAttribute("aria-label", stringDifferent);
removeThenSet(inputDifferent);
ready
set only, same
inputSame.setAttribute("aria-label", stringSame);
inputDifferent.setAttribute("aria-label", stringDifferent);
setOnly(inputSame);
ready
set only, different
inputSame.setAttribute("aria-label", stringSame);
inputDifferent.setAttribute("aria-label", stringDifferent);
setOnly(inputDifferent);
ready
set if different, same
inputSame.setAttribute("aria-label", stringSame);
inputDifferent.setAttribute("aria-label", stringDifferent);
setIfDifferent(inputSame);
ready
set if different, different
inputSame.setAttribute("aria-label", stringSame);
inputDifferent.setAttribute("aria-label", stringDifferent);
setIfDifferent(inputDifferent);
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