switch vs map

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
switch
function statusTagHtml(status) {
    let colour
    let text

    switch (status) {
      case 'assessment_started':
        colour = 'yellow'
        text = 'Assessment started'
        break
      case 'awaiting_assesment':
        colour = 'orange'
        text = 'Awaiting assessment'
        break
      case 'referral_submitted':
        colour = 'red'
        text = 'Referral submitted'
        break
      default:
        colour = 'grey'
        text = status
        break
    }

    return `<strong class="govuk-tag govuk-tag--${colour}">${text}</strong>`
  }
  
statusTagHtml('referral_submitted')
ready
map
function statusTagHtml(status) {
    const statusMap = {
      assessment_started: {
        colour: 'yellow',
        text: 'Assessment started',
      },
      awaiting_assesment: {
        colour: 'orange',
        text: 'Awaiting assessment',
      },
      referral_started: {
        colour: 'grey',
        text: 'Referral started',
      },
      referral_submitted: {
        colour: 'red',
        text: 'Referral submitted',
      }
    }

    const colour = statusMap[status]?.colour || 'grey'
    const text = statusMap[status]?.text || status

    return `<strong class="govuk-tag govuk-tag--${colour}">${text}</strong>`
  }
  
statusTagHtml('referral_submitted')
ready

Revisions

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