RegExp vs substring

JavaScript performance comparison

Revision 3 of this 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
var response;
if ((response = maskregex.exec(hostmask))) {
  obj.nick = response[1];
  obj.user = response[2];
  obj.host = response[3];
}
pending…
substring
var index1 = hostmask.indexOf('!');
var index2 = hostmask.indexOf('@');
obj.nick = hostmask.substring(0, index1);
obj.user = hostmask.substring(index1 + 1, index2);
obj.host = hostmask.substring(index2 + 1);
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