/regex/i vs toLowerCase

Benchmark created on


Setup

function matchRegex(method) {
      var r = RegExp('^'+method+'$','i');
      return function(str) {
        return r.exec(str) != null;
      }
    }
    
    function matchLower(method) {
      var t = method.toLowerCase();
      return function(str) {
        return str.toLowerCase() === t;
      }
    }
    
    function ok(t) {
      if(!t) throw new Error('Assert '+t);
    }
    
    var matchRGet = matchRegex('get');
    var matchLGet = matchLower('get');

Test runner

Ready to run.

Testing in
TestOps/sec
regex
ok(matchRGet('GET'));
ok(matchRGet('get'));
ok(!matchRGet('POST'));
ready
lower
ok(matchLGet('GET'));
ok(matchLGet('get'));
ok(!matchLGet('POST'));
ready

Revisions

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