switch vs object

Benchmark created on


Setup

function getSwitch (name) {
	switch (name) {
    case 'broadcast': return 'mdiBullhorn'
    case 'clean': return 'mdiDelete'
    case 'editRuntime': return 'mdiPlaylistEdit'
    case 'hold': return 'mdiPauseCircleOutline' // to distinguish from pause
    case 'kill': return 'mdiCloseCircle'
    case 'log': return 'mdiFileDocumentOutline'
    case 'message': return 'mdiEmail'
    case 'pause': return 'mdiPause'
    case 'play': return 'mdiPlay'
    case 'poll': return 'mdiRefreshCircle'
    case 'release': return 'mdiPlayCircleOutline' // to distinguish from play
    case 'reload': return 'mdiReload'
    case 'remove': return 'mdiMinusCircleOutline'
    case 'resume': return 'mdiPlay'
    case 'set': return 'mdiVectorPolylineEdit'
    case 'stop': return 'mdiStop'
    case 'trigger': return 'mdiCursorPointer'
    default: return 'mdiCog'
  }
}

const mutationIcons = {
  broadcast: 'mdiBullhorn',
  clean: 'mdiDelete',
  editRuntime: 'mdiPlaylistEdit',
  hold: 'mdiPauseCircleOutline', // to distinguish from pause
  kill: 'mdiCloseCircle',
  log: 'mdiFileDocumentOutline',
  message: 'mdiEmail',
  pause: 'mdiPause',
  play: 'mdiPlay',
  poll: 'mdiRefreshCircle',
  release: 'mdiPlayCircleOutline', // to distinguish from play
  reload: 'mdiReload',
  remove: 'mdiMinusCircleOutline',
  resume: 'mdiPlay',
  set: 'mdiVectorPolylineEdit',
  stop: 'mdiStop',
  trigger: 'mdiCursorPointer'
}

function getObj (name) {
	return mutationIcons[name] ?? 'mdiCog'
}

const getObj2 = (name) => (
  {
    broadcast: 'mdiBullhorn',
    clean: 'mdiDelete',
    editRuntime: 'mdiPlaylistEdit',
    hold: 'mdiPauseCircleOutline', // to distinguish from pause
    kill: 'mdiCloseCircle',
    log: 'mdiFileDocumentOutline',
    message: 'mdiEmail',
    pause: 'mdiPause',
    play: 'mdiPlay',
    poll: 'mdiRefreshCircle',
    release: 'mdiPlayCircleOutline', // to distinguish from play
    reload: 'mdiReload',
    remove: 'mdiMinusCircleOutline',
    resume: 'mdiPlay',
    set: 'mdiVectorPolylineEdit',
    stop: 'mdiStop',
    trigger: 'mdiCursorPointer'
  }[name] ?? 'mdiCog'
)

Test runner

Ready to run.

Testing in
TestOps/sec
switch
getSwitch('stop')
ready
obj
getObj('stop')
ready
obj2
getObj2('stop')
ready

Revisions

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