switch vs if for boolean default (v2)

Revision 2 of this benchmark created on


Preparation HTML

<script>
  var rand=0;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
switch true
switch (true) {
case (rand === 5):
  break;
case (rand < 5):
  break;
case (rand < 4):
  break;
case (rand < 3):
  break;
case (rand < 2):
  break;
default:
  break;
}
ready
switch false
switch (false) {
case (rand === 5):
  break;
case (rand < 5):
  break;
case (rand < 4):
  break;
case (rand < 3):
  break;
case (rand < 2):
  break;
default:
  break;
}
ready
if true
if (rand === 5) {} else if (rand < 5) {

} else if (rand < 4) {

} else if (rand < 3) {

} else if (rand < 2) {

} else {

}
ready
if false
if (!(rand === 5)) {} else if (!(rand < 5)) {

} else if (!(rand < 4)) {

} else if (!(rand < 3)) {

} else if (!(rand < 2)) {

} else {

}
ready

Revisions

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