in operator vs indexOf vs Set has

JavaScript performance comparison

Test case created by CyberAP

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const data = [
    'html',
    'body',
    'base',
    'head',
    'link',
    'meta',
    'style',
    'title',
    'address',
    'article',
    'aside',
    'footer',
    'header',
    'h1',
    'h2',
    'h3',
    'h4',
    'h5',
    'h6',
    'hgroup',
    'nav',
    'section',
    'div',
    'dd',
    'dl',
    'dt',
    'figcaption',
    'figure',
    'picture',
    'hr',
    'img',
    'li',
    'main',
    'ol',
    'p',
    'pre',
    'ul',
    'a',
    'b',
    'abbr',
    'bdi',
    'bdo',
    'br',
    'cite',
    'code',
    'data',
    'dfn',
    'em',
    'i',
    'kbd',
    'mark',
    'q',
    'rp',
    'rt',
    'rtc',
    'ruby',
    's',
    'samp',
    'small',
    'span',
    'strong',
    'sub',
    'sup',
    'time',
    'u',
    'var',
    'wbr',
    'area',
    'audio',
    'map',
    'track',
    'video',
    'embed',
    'object',
    'param',
    'source',
    'canvas',
    'script',
    'noscript',
    'del',
    'ins',
    'caption',
    'col',
    'colgroup',
    'table',
    'thead',
    'tbody',
    'td',
    'th',
    'tr',
    'button',
    'datalist',
    'fieldset',
    'form',
    'input',
    'label',
    'legend',
    'meter',
    'optgroup',
    'option',
    'output',
    'progress',
    'select',
    'textarea',
    'details',
    'dialog',
    'menu',
    'menuitem',
    'summary',
    'content',
    'element',
    'shadow',
    'template',
    'blockquote',
    'iframe',
    'tfoot'
  ]
  
  const set = new Set(data)
  const object = data.reduce((acc, el) => (acc[el] = true, acc), {})

};
</script>

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
direct property access
object['html'];
object['dfn'];
object['tfoot'];

object['fail'];
pending…
in operator
'html' in object;
'dfn' in object;
'tfoot' in object;

'fail' in object;
pending…
indexOf
data.indexOf('html') !== -1;
data.indexOf('dfn') !== -1;
data.indexOf('tfoot') !== -1;

data.indexOf('fail') !== -1;
pending…
Set.prototype.has
set.has('html');
set.has('dfn');
set.has('tfoot');

set.has('fail');
pending…

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

Compare results of other browsers

0 Comments