switch vs object

JavaScript performance comparison

Test case created by davidmarkclements

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
switch
function mapping (key) {
  switch (key) {
    case 'className': return 'class'
    case 'htmlFor': return 'for'
    case 'defaultChecked': return 'checked'
    case 'defaultValue': 'value'
    case 'contentEditable': return 'contenteditable'
    case 'crossOrigin': return 'crossorigin'
    case 'spellCheck': return 'spellcheck'
    case 'allowFullScreen': return 'allowfullscreen'
    case 'autoPlay': return 'autoplay'
    case 'autoFocus': return 'autofocus'
    case 'formNoValidate': return 'formnovalidate'
    case 'noModule': return 'nomodule'
    case 'noValidate': return 'novalidate'
    case 'playsInline': return 'playsinline'
    case 'readOnly': return 'readonly'
    case 'rowSpan': return 'rowspan'
    case 'itemScope': return 'itemscope'
    case 'tabIndex': return 'tabindex'
    // hyphenated
    case 'httpEquiv': return 'http-equiv'
    case 'acceptCharset': return 'accept-charset'
    case 'accentHeight': return 'accent-height'
    case 'alignmentBaseline': return 'alignment-baseline'
    case 'arabicForm': return 'arabic-form'
    case 'baselineShift': return 'baseline-shift'
    case 'capHeight': return 'cap-height'
    case 'clipPath': return 'clip-path'
    case 'clipRule': return 'clip-rule'
    case 'colorInterpolation': return 'color-interpolation'
    case 'colorInterpolationFilters': return 'color-interpolation-filters'
    case 'colorProfile': return 'color-profile'
    case 'colorRendering': return 'color-rendering'
    case 'dominantBaseline': return 'dominant-baseline'
    case 'enableBackground': return 'enable-background'
    case 'fillOpacity': return 'fill-opacity'
    case 'fillRule': return 'fill-rule'
    case 'floodColor': return 'flood-color'
    case 'floodOpacity': return 'flood-opacity'
    case 'fontFamily': return 'font-family'
    case 'fontSize': return 'font-size'
    case 'fontSizeAdjust': return 'font-size-adjust'
    case 'fontStretch': return 'font-stretch'
    case 'fontStyle': return 'font-style'
    case 'fontVariant': return 'font-variant'
    case 'fontWeight': return 'font-weight'
    case 'glyphName': return 'glyph-name'
    case 'glyphOrientationHorizontal': return 'glyph-orientation-horizontal'
    case 'glyphOrientationVertical': return 'glyph-orientation-vertical'
    case 'horizAdvX': return 'horiz-adv-x'
    case 'horizOriginX': return 'horiz-origin-x'
    case 'imageRendering': return 'image-rendering'
    case 'letterSpacing': return 'letter-spacing'
    case 'lightingColor': return 'lighting-color'
    case 'markerEnd': return 'marker-end'
    case 'markerMid': return 'marker-mid'
    case 'markerStart': return 'marker-start'
    case 'overlinePosition': return 'overline-position'
    case 'overlineThickness': return 'overline-thickness'
    case 'paintOrder': return 'paint-order'
    case 'panose-1': return 'panose-1'
    case 'pointerEvents': return 'pointer-events'
    case 'renderingIntent': return 'rendering-intent'
    case 'shapeRendering': return 'shape-rendering'
    case 'stopColor': return 'stop-color'
    case 'stopOpacity': return 'stop-opacity'
    case 'strikethroughPosition': return 'strikethrough-position'
    case 'strikethroughThickness': return 'strikethrough-thickness'
    case 'strokeDasharray': return 'stroke-dasharray'
    case 'strokeDashoffset': return 'stroke-dashoffset'
    case 'strokeLinecap': return 'stroke-linecap'
    case 'strokeLinejoin': return 'stroke-linejoin'
    case 'strokeMiterlimit': return 'stroke-miterlimit'
    case 'strokeOpacity': return 'stroke-opacity'
    case 'strokeWidth': return 'stroke-width'
    case 'textAnchor': return 'text-anchor'
    case 'textDecoration': return 'text-decoration'
    case 'textRendering': return 'text-rendering'
    case 'underlinePosition': return 'underline-position'
    case 'underlineThickness': return 'underline-thickness'
    case 'unicodeBidi': return 'unicode-bidi'
    case 'unicodeRange': return 'unicode-range'
    case 'unitsPerEm': return 'units-per-em'
    case 'vAlphabetic': return 'v-alphabetic'
    case 'vHanging': return 'v-hanging'
    case 'vIdeographic': return 'v-ideographic'
    case 'vMathematical': return 'v-mathematical'
    case 'vectorEffect': return 'vector-effect'
    case 'vertAdvY': return 'vert-adv-y'
    case 'vertOriginX': return 'vert-origin-x'
    case 'vertOriginY': return 'vert-origin-y'
    case 'wordSpacing': return 'word-spacing'
    case 'writingMode': return 'writing-mode'
    case 'xHeight': return 'x-height'
    // xml namespace
    case 'xmlnsXlink': return 'xmlns:xlink'
    case 'xmlBase': return 'xml:base'
    case 'xmlLang': return 'xml:lang'
    case 'xmlSpace': return 'xml:space'
    case  'xlinkActuate': return 'xlink:actuate'
    case  'xlinkArcrole': return 'xlink:arcrole'
    case  'xlinkHref': return 'xlink:href'
    case  'xlinkRole': return 'xlink:role'
    case  'xlinkShow': return 'xlink:show'
    case  'xlinkTitle': return 'xlink:title'
    case  'xlinkType': return 'xlink:type'
    default: return key
  }
}

mapping('className')
mapping('htmlFor')
mapping('defaultChecked')
mapping('defaultValue')
mapping('contentEditable')
mapping('crossOrigin')
mapping('spellCheck')
mapping('allowFullScreen')
mapping('autoPlay')
mapping('autoFocus')
mapping('formNoValidate')
mapping('noModule')
mapping('noValidate')
mapping('playsInline')
mapping('readOnly')
mapping('rowSpan')
mapping('itemScope')
mapping('tabIndex')
mapping('httpEquiv')
mapping('acceptCharset')
mapping('accentHeight')
mapping('alignmentBaseline')
mapping('arabicForm')
mapping('baselineShift')
mapping('capHeight')
mapping('clipPath')
mapping('clipRule')
mapping('colorInterpolation')
mapping('colorInterpolationFilters')
mapping('colorProfile')
mapping('colorRendering')
mapping('dominantBaseline')
mapping('enableBackground')
mapping('fillOpacity')
mapping('fillRule')
mapping('floodColor')
mapping('floodOpacity')
mapping('fontFamily')
mapping('fontSize')
mapping('fontSizeAdjust')
mapping('fontStretch')
mapping('fontStyle')
mapping('fontVariant')
mapping('fontWeight')
mapping('glyphName')
mapping('glyphOrientationHorizontal')
mapping('glyphOrientationVertical')
mapping('horizAdvX')
mapping('horizOriginX')
mapping('imageRendering')
mapping('letterSpacing')
mapping('lightingColor')
mapping('markerEnd')
mapping('markerMid')
mapping('markerStart')
mapping('overlinePosition')
mapping('overlineThickness')
mapping('paintOrder')
mapping('panose-1')
mapping('pointerEvents')
mapping('renderingIntent')
mapping('shapeRendering')
mapping('stopColor')
mapping('stopOpacity')
mapping('strikethroughPosition')
mapping('strikethroughThickness')
mapping('strokeDasharray')
mapping('strokeDashoffset')
mapping('strokeLinecap')
mapping('strokeLinejoin')
mapping('strokeMiterlimit')
mapping('strokeOpacity')
mapping('strokeWidth')
mapping('textAnchor')
mapping('textDecoration')
mapping('textRendering')
mapping('underlinePosition')
mapping('underlineThickness')
mapping('unicodeBidi')
mapping('unicodeRange')
mapping('unitsPerEm')
mapping('vAlphabetic')
mapping('vHanging')
mapping('vIdeographic')
mapping('vMathematical')
mapping('vectorEffect')
mapping('vertAdvY')
mapping('vertOriginX')
mapping('vertOriginY')
mapping('wordSpacing')
mapping('writingMode')
mapping('xHeight')
mapping('xmlnsXlink')
mapping('xmlBase')
mapping('xmlLang')
mapping('xmlSpace')
mapping('xlinkActuate')
mapping('xlinkArcrole')
mapping('xlinkHref')
mapping('xlinkRole')
mapping('xlinkShow')
mapping('xlinkTitle')
mapping('xlinkType')
mapping('other')
pending…
object
const o = {
  'className': 'class',
  'htmlFor': 'for',
  'defaultChecked': 'checked',
  'defaultValue': 'value',
  'contentEditable': 'contenteditable',
  'crossOrigin': 'crossorigin',
  'spellCheck': 'spellcheck',
  'allowFullScreen': 'allowfullscreen',
  'autoPlay': 'autoplay',
  'autoFocus': 'autofocus',
  'formNoValidate': 'formnovalidate',
  'noModule': 'nomodule',
  'noValidate': 'novalidate',
  'playsInline': 'playsinline',
  'readOnly': 'readonly',
  'rowSpan': 'rowspan',
  'itemScope': 'itemscope',
  'tabIndex': 'tabindex',
  'httpEquiv': 'http-equiv',
  'acceptCharset': 'accept-charset',
  'accentHeight': 'accent-height',
  'alignmentBaseline': 'alignment-baseline',
  'arabicForm': 'arabic-form',
  'baselineShift': 'baseline-shift',
  'capHeight': 'cap-height',
  'clipPath': 'clip-path',
  'clipRule': 'clip-rule',
  'colorInterpolation': 'color-interpolation',
  'colorInterpolationFilters': 'color-interpolation-filters',
  'colorProfile': 'color-profile',
  'colorRendering': 'color-rendering',
  'dominantBaseline': 'dominant-baseline',
  'enableBackground': 'enable-background',
  'fillOpacity': 'fill-opacity',
  'fillRule': 'fill-rule',
  'floodColor': 'flood-color',
  'floodOpacity': 'flood-opacity',
  'fontFamily': 'font-family',
  'fontSize': 'font-size',
  'fontSizeAdjust': 'font-size-adjust',
  'fontStretch': 'font-stretch',
  'fontStyle': 'font-style',
  'fontVariant': 'font-variant',
  'fontWeight': 'font-weight',
  'glyphName': 'glyph-name',
  'glyphOrientationHorizontal': 'glyph-orientation-horizontal',
  'glyphOrientationVertical': 'glyph-orientation-vertical',
  'horizAdvX': 'horiz-adv-x',
  'horizOriginX': 'horiz-origin-x',
  'imageRendering': 'image-rendering',
  'letterSpacing': 'letter-spacing',
  'lightingColor': 'lighting-color',
  'markerEnd': 'marker-end',
  'markerMid': 'marker-mid',
  'markerStart': 'marker-start',
  'overlinePosition': 'overline-position',
  'overlineThickness': 'overline-thickness',
  'paintOrder': 'paint-order',
  'panose-1': 'panose-1',
  'pointerEvents': 'pointer-events',
  'renderingIntent': 'rendering-intent',
  'shapeRendering': 'shape-rendering',
  'stopColor': 'stop-color',
  'stopOpacity': 'stop-opacity',
  'strikethroughPosition': 'strikethrough-position',
  'strikethroughThickness': 'strikethrough-thickness',
  'strokeDasharray': 'stroke-dasharray',
  'strokeDashoffset': 'stroke-dashoffset',
  'strokeLinecap': 'stroke-linecap',
  'strokeLinejoin': 'stroke-linejoin',
  'strokeMiterlimit': 'stroke-miterlimit',
  'strokeOpacity': 'stroke-opacity',
  'strokeWidth': 'stroke-width',
  'textAnchor': 'text-anchor',
  'textDecoration': 'text-decoration',
  'textRendering': 'text-rendering',
  'underlinePosition': 'underline-position',
  'underlineThickness': 'underline-thickness',
  'unicodeBidi': 'unicode-bidi',
  'unicodeRange': 'unicode-range',
  'unitsPerEm': 'units-per-em',
  'vAlphabetic': 'v-alphabetic',
  'vHanging': 'v-hanging',
  'vIdeographic': 'v-ideographic',
  'vMathematical': 'v-mathematical',
  'vectorEffect': 'vector-effect',
  'vertAdvY': 'vert-adv-y',
  'vertOriginX': 'vert-origin-x',
  'vertOriginY': 'vert-origin-y',
  'wordSpacing': 'word-spacing',
  'writingMode': 'writing-mode',
  'xHeight': 'x-height',
  'xmlnsXlink': 'xmlns:xlink',
  'xmlBase': 'xml:base',
  'xmlLang': 'xml:lang',
  'xmlSpace': 'xml:space',
    'xlinkActuate': 'xlink:actuate',
    'xlinkArcrole': 'xlink:arcrole',
    'xlinkHref': 'xlink:href',
    'xlinkRole': 'xlink:role',
    'xlinkShow': 'xlink:show',
    'xlinkTitle': 'xlink:title',
    'xlinkType': 'xlink:type'
}

function mapping (key) {
  return o[key]
}
mapping('className')
mapping('htmlFor')
mapping('defaultChecked')
mapping('defaultValue')
mapping('contentEditable')
mapping('crossOrigin')
mapping('spellCheck')
mapping('allowFullScreen')
mapping('autoPlay')
mapping('autoFocus')
mapping('formNoValidate')
mapping('noModule')
mapping('noValidate')
mapping('playsInline')
mapping('readOnly')
mapping('rowSpan')
mapping('itemScope')
mapping('tabIndex')
mapping('httpEquiv')
mapping('acceptCharset')
mapping('accentHeight')
mapping('alignmentBaseline')
mapping('arabicForm')
mapping('baselineShift')
mapping('capHeight')
mapping('clipPath')
mapping('clipRule')
mapping('colorInterpolation')
mapping('colorInterpolationFilters')
mapping('colorProfile')
mapping('colorRendering')
mapping('dominantBaseline')
mapping('enableBackground')
mapping('fillOpacity')
mapping('fillRule')
mapping('floodColor')
mapping('floodOpacity')
mapping('fontFamily')
mapping('fontSize')
mapping('fontSizeAdjust')
mapping('fontStretch')
mapping('fontStyle')
mapping('fontVariant')
mapping('fontWeight')
mapping('glyphName')
mapping('glyphOrientationHorizontal')
mapping('glyphOrientationVertical')
mapping('horizAdvX')
mapping('horizOriginX')
mapping('imageRendering')
mapping('letterSpacing')
mapping('lightingColor')
mapping('markerEnd')
mapping('markerMid')
mapping('markerStart')
mapping('overlinePosition')
mapping('overlineThickness')
mapping('paintOrder')
mapping('panose-1')
mapping('pointerEvents')
mapping('renderingIntent')
mapping('shapeRendering')
mapping('stopColor')
mapping('stopOpacity')
mapping('strikethroughPosition')
mapping('strikethroughThickness')
mapping('strokeDasharray')
mapping('strokeDashoffset')
mapping('strokeLinecap')
mapping('strokeLinejoin')
mapping('strokeMiterlimit')
mapping('strokeOpacity')
mapping('strokeWidth')
mapping('textAnchor')
mapping('textDecoration')
mapping('textRendering')
mapping('underlinePosition')
mapping('underlineThickness')
mapping('unicodeBidi')
mapping('unicodeRange')
mapping('unitsPerEm')
mapping('vAlphabetic')
mapping('vHanging')
mapping('vIdeographic')
mapping('vMathematical')
mapping('vectorEffect')
mapping('vertAdvY')
mapping('vertOriginX')
mapping('vertOriginY')
mapping('wordSpacing')
mapping('writingMode')
mapping('xHeight')
mapping('xmlnsXlink')
mapping('xmlBase')
mapping('xmlLang')
mapping('xmlSpace')
mapping('xlinkActuate')
mapping('xlinkArcrole')
mapping('xlinkHref')
mapping('xlinkRole')
mapping('xlinkShow')
mapping('xlinkTitle')
mapping('xlinkType')
mapping('other')
pending…

Compare results of other browsers

Revisions

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

0 Comments