RegExp test vs looping indexOf

Benchmark created by grammarxcore on


Setup

var notAcceptedArray = ['<', '>', '[', ']', '(', ')', ',', ';', '/', '\\', '\'', '"'];
  
  function checkAgainstNotAcceptedArray(str) {
      for (var i = 0; i < notAcceptedArray.length; i++) {
          if (str.indexOf(notAcceptedArray[i]) > -1)
              return false;
      };
  
      return true;
  };
  
  var notAcceptedRegExp = /[<>\(\)\[\],;\\\/'"]/;
  
  function checkAgainstNotAcceptedRegExp(str) {
      return notAcceptedRegExp.test(str);
  }
  
  var acceptedRegExp = /^[\w\- ]+$/;
  
  function checkAgainstAcceptedRegExp(str) {
      return acceptedRegExp.test(str);
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Array success
checkAgainstNotAcceptedArray("good command");
ready
Array failure from start
checkAgainstNotAcceptedArray("bad <> command");
ready
Array failure from end
checkAgainstNotAcceptedArray("bad ' command");
ready
RegExp success against not accepted
checkAgainstNotAcceptedRegExp("good command");
ready
RegExp failure against not accepted from start of character set
checkAgainstNotAcceptedRegExp("bad <> command");
ready
RegExp failure against not accepted from end character set
checkAgainstNotAcceptedRegExp("bad ' command");
ready
RegExp success against accepted
checkAgainstAcceptedRegExp("good command");
ready
RegExp failure against accepted
checkAgainstAcceptedRegExp("bad <> command");
ready
RegExp failure against accepted round 2
checkAgainstAcceptedRegExp("bad ' command");
ready

Revisions

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

  • Revision 1: published by grammarxcore on