remove attribute vs set to false (v4)

Revision 4 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">
<input id="inputUndefined" type="text" value="The quick brown fox jumps over the lazy dog">
<script>
  var inputSame = document.getElementById("inputSame");
  var inputDifferent = document.getElementById("inputDifferent");
  var inputUndefined = document.getElementById("inputUndefined");
  var stringSame = "same";
  var stringDifferent = "different";

  var removeThenSet = function(input) {
      input.removeAttribute("aria-sort");
      input.setAttribute("aria-sort", stringSame);
      }
      
  var setOnly = function(input) {
      input.setAttribute("aria-sort", stringSame);
      }
      
      
      
  var removeOnly = function(input, logSomething) {
      input.removeAttribute("aria-sort");
      }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
remove then set, same
inputSame.setAttribute("aria-sort", stringSame);
removeThenSet(inputSame);
ready
remove then set, different
inputSame.setAttribute("aria-sort", stringDifferent);
removeThenSet(inputSame);
ready
set only, same
inputSame.setAttribute("aria-sort", stringSame);
setOnly(inputSame);
ready
set only, different
inputSame.setAttribute("aria-sort", stringDifferent);
setOnly(inputSame);
ready
remove only, defined
inputSame.setAttribute("aria-sort", stringSame);
removeOnly(inputSame);
ready
remove only, undefined
inputUndefined.removeAttribute("aria-sort");
removeOnly(inputUndefined);
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