startsWith vs RegExp

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function startsWith(str, starts) {
      if (starts === '') {
        return true;
      }
   
      if (str == null || starts == null) {
        return false;
      }
   
      str = String(str);
      starts = String(starts);
   
      return str.length >= starts.length && str.slice(0, starts.length) === starts;
    }
   
    var re = /^~/;
   
    var shortFalse = '';
    var longFalse = 'PfZoqudhclAUucJtGqgNRjNv5TQ3iXGCKuiWizzzbtebTcmPfYgi4VSpixKDk6ET5VQQOb9xyBU8ys8D5IqFm1Siww8VkEHJtMJ1DlmtSSThIW0VpUeR5cg1AhxOcaoSjLD50qVkef9EbW1Z3mobq4hfdhvlQex9TOejiQgLLdKoHYCac4pGs1qDmbnCwHWXYB4F5PTG';
   
    var shortTrue = '~' + shortFalse;
    var longTrue = '~' + longFalse;
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
startsWith shortTrue
startsWith(shortTrue, '~');
pending…
RegExp shortTrue
re.test(shortTrue);
pending…
startWith shortFalse
startsWith(shortFalse, '~');
pending…
RegExp shortFalse
re.test(shortFalse);
pending…
startsWith longFalse
startsWith(longFalse, '~');
pending…
RegExp longFalse
re.test(longFalse);
pending…
startsWith longTrue
startsWith(longTrue, '~');
pending…
RegExp longTrue
re.test(longTrue);
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

Add a comment