Character counting

JavaScript performance comparison

Test case created by Jared Wein

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var s = "http://msujaws.wordpress.com/2012/09/18/unnecessary-regular-expressions/#comments";
};
</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 character counting
var t = s.replace(/[^.]*/g, "").length;
pending…
Iterative character counting
var j, l = s.length, count = 0;
for (j = 0; j < l; j++) {
  if (s[j] == '.')
    count++;
}
pending…
Iterative character counting (no coercion)
var j, l = s.length, count = 0;
for (j = 0; j < l; j++) {
  if (s[j] === '.')
    count++;
}
pending…
Iterative character counting (prefix increment)
var j, l = s.length, count = 0;
for (j = 0; j < l; ++j) {
  if (s[j] == '.')
    ++count;
}
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