RegExp vs substring

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var hostmask = "rsh!~king@192.168.host.mask";
   
    var maskregex = /^([^!]+)!([^@]+)@(.*)$/;
   
    var obj = {
      host: '',
      nick: '',
      user: ''
    };
};
</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
regex
if (maskregex.test(hostmask)) {
  var response = maskregex.exec(hostmask);
  obj.nick = response[1];
  obj.user = response[2];
  obj.host = response[3];
}
pending…
substring
obj.nick = hostmask.substring(0, hostmask.indexOf('!'));
obj.user = hostmask.substring(hostmask.indexOf('!') + 1, hostmask.indexOf('@'));
obj.host = hostmask.substring(hostmask.indexOf('@') + 1, hostmask.length);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment