Switch vs IfElse

Benchmark created on


Setup

const tag = {
	config: {
		assetAlarms: {
			enabled: false,
			pending: false,
		},
		burstMode: {
			enabled: false,
			pending: false
		},
		beaconMode: {
			enabled: false,
			pending: false
		}
	}
}

Test runner

Ready to run.

Testing in
TestOps/sec
IfElse
 const action = 'enableBeacon';
 if (action === 'disableAlarm') {
      tag.config.assetAlarms.enabled = false;
      tag.config.assetAlarms.pending = true;
    } else if (action === 'enableAlarm') {
      tag.config.assetAlarms.enabled = true;
      tag.config.assetAlarms.pending = true;
    } else if (action === 'enableBurst') {
      tag.config.burstMode.enabled = true;
      tag.config.burstMode.pending = true;
    } else if (action === 'disableBurst') {
      tag.config.burstMode.enabled = false;
      tag.config.burstMode.pending = true;
    } else if (action === 'enableBeacon') {
      tag.config.beaconMode.enabled = true;
      tag.config.beaconMode.pending = true;
    } else if (action === 'disableBeacon') {
      tag.config.beaconMode.enabled = false;
      tag.config.beaconMode.pending = true;
    }
ready
Switch
 const action = 'enableBeacon';
switch(action) {
	case 'disableAlarm':
		tag.config.assetAlarms.enabled = false;
      tag.config.assetAlarms.pending = true;
      break;
    case 'enableAlarm':
      tag.config.assetAlarms.enabled = true;
      tag.config.assetAlarms.pending = true;
    case 'enableBurst':
      tag.config.burstMode.enabled = true;
      tag.config.burstMode.pending = true;
      break;
    case 'disableBurst':
      tag.config.burstMode.enabled = false;
      tag.config.burstMode.pending = true;
      break;
    case 'enableBeacon':
      tag.config.beaconMode.enabled = true;
      tag.config.beaconMode.pending = true;
    case 'disableBeacon':
      tag.config.beaconMode.enabled = false;
      tag.config.beaconMode.pending = true;
    default:
    	break;
}
ready

Revisions

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