contains, substring vs replace

JavaScript performance comparison

Test case created by Sean

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var a = [];
    for (var i = 0; i < 1000; i++) {
      a.push('1234' + i.toString());
    }
};
</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
contains substring
for (i = 0; a.length; i++) {
  var b = a[i].toString().contains('1234');
  var c;
  if (b) {
    c.substr(4);
  }
}
pending…
replace
for (i = 0; a.length; i++) {
  var b = a[i].toString().replace('1234', '');
}
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