Alternative Match

Benchmark created by Denis G. on


Description

This simple test aims to compare usage of a unique RegExp or multiple IndexOf to check presence of some substrings into a string. Three pair of test is presented for respectively one substring, two substrings and four substrings.

Matches are done on several part of the string and several orders to avoid any advantages one or the other could have by the position of the match in the string, or the order of the alternatives.

Preparation HTML

<script>
  var s = 'The Quick Brown Fox Jumps Over The Lazy Dog',
      f;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
IndexOf
f = s.indexOf('The') > -1
f = s.indexOf('Jumps') > -1
f = s.indexOf('Dog') > -1
f = s.indexOf('Yellow') > -1
ready
RegEx
f = /The/.test(s);
f = /Jumps/.test(s);
f = /Dog/.test(s);
f = /Yellow/.test(s);
ready
IndexOf - Two Alternatives
f = s.indexOf('The') > -1 || s.indexOf('Yellow') > -1
f = s.indexOf('Yellow') > -1 || s.indexOf('The') > -1
f = s.indexOf('Jumps') > -1 || s.indexOf('Yellow') > -1
f = s.indexOf('Yellow') > -1 || s.indexOf('Jumps') > -1
f = s.indexOf('Dog') > -1 || s.indexOf('Yellow') > -1
f = s.indexOf('Yellow') > -1 || s.indexOf('Dog') > -1
f = s.indexOf('Yellow') > -1 || s.indexOf('Red') > -1
f = s.indexOf('Red') > -1 || s.indexOf('Yellow') > -1
ready
RegEx - Two Alternatives
f = /The|Yellow/.test(s);
f = /Yellow|The/.test(s);
f = /Jumps|Yellow/.test(s);
f = /Yellow|Jumps/.test(s);
f = /Dog|Yellow/.test(s);
f = /Yellow|Dog/.test(s);
f = /Yellow|Red/.test(s);
f = /Red|Yellow/.test(s);
ready
IndexOf - Four Alternatives
f = s.indexOf('The') > -1 || s.indexOf('Yellow') > -1 || s.indexOf('Quick') > -1 || s.indexOf('Slow') > -1;
f = s.indexOf('Slow') > -1 || s.indexOf('Quick') > -1 || s.indexOf('Yellow') > -1 || s.indexOf('The') > -1;
f = s.indexOf('Fox') > -1 || s.indexOf('Yellow') > -1 || s.indexOf('Jumps') > -1 || s.indexOf('Slow') > -1;
f = s.indexOf('Slow') > -1 || s.indexOf('Jumps') > -1 || s.indexOf('Yellow') > -1 || s.indexOf('Fox') > -1;
f = s.indexOf('Dog') > -1 || s.indexOf('Yellow') > -1 || s.indexOf('Lazy') > -1 || s.indexOf('Slow') > -1;
f = s.indexOf('Slow') > -1 || s.indexOf('Lazy') > -1 || s.indexOf('Yellow') > -1 || s.indexOf('Dog') > -1;
f = s.indexOf('Slow') > -1 || s.indexOf('Easy') > -1 || s.indexOf('Yellow') > -1 || s.indexOf('Red') > -1;
f = s.indexOf('Red') > -1 || s.indexOf('Yellow') > -1 || s.indexOf('Easy') > -1 || s.indexOf('Slow') > -1;
ready
RegEx - Four Alternatives
f = /The|Yellow|Quick|Slow/.test(s);
f = /Slow|Quick|Yellow|The/.test(s);
f = /Fox|Yellow|Jumps|Slow/.test(s);
f = /Slow|Jumps|Yellow|Fox/.test(s);
f = /Dog|Yellow|Lazy|Slow/.test(s);
f = /Slow|Lazy|Yellow|Dog/.test(s);
f = /Slow|Easy|Yellow|Red/.test(s);
f = /Red|Yellow|Easy|Slow/.test(s);
ready

Revisions

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

  • Revision 1: published by Denis G. on