indexof vs regex

Benchmark created by Tim Schottler on


Preparation HTML

<div id="root">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <span>tacos</span>
    <span>tacos</span>
    <span>tacos</span>
    <span>tacos</span>
    <span>tacos</span>
    <div></div>
    <div>
    <span>tacos</span>
    <span>tacos</span>
    <span>tacos</span>
    <span>tacos</span>
    <span>tacos</span>
    <span>tacos</span>
    <span>tacos</span>
    <span>tacos</span>
    </div>
    <div></div>
    <div></div>
    <p>tacos</p>
    <h1>tacos</h1>
    <h2>tacos</h2>
    <span>tacos</span>
    <span>tacos</span>
    <span>tacos</span>
    <span>tacos</span>
    <span>tacos</span>
    <span>tacos</span>
    <h3>tacos</h3>
    <h4>tacos</h4>
    <h5>tacos</h5>
    <h6>tacos</h6>
    <span>tacos</span>
    <span>tacos</span>
    <span>tacos</span>
    <span>tacos</span>
    <span>tacos</span>
</div>

Setup

var nodes = document.getElementById('root').children;
  var results = [];
  var regex = /^(DIV|P|H[1-6])$/;
  var validNodeNames = ['DIV', 'P', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6'];

Test runner

Ready to run.

Testing in
TestOps/sec
regex
for(var i=0; i<nodes.length; i++){
  results.push(regex.test(nodes[i].nodeName));
}
ready
indexof
for(var i=0; i<nodes.length; i++){
  results.push(validNodeNames.indexOf(nodes[i].nodeName) > -1);
}
ready

Revisions

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

  • Revision 1: published by Tim Schottler on