count string occurrence in string

JavaScript performance comparison

Revision 12 of this test case created

Info

Count string occurrence in string.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis lacus tortor, dignissim ut eleifend ut, dignissim in dolor. In sit amet eros vulputate nulla pulvinar volutpat vitae eu libero. Fusce ligula metus, dictum vitae scelerisque ac, fermentum ac dolor. Donec nec sapien dui. Proin vel lorem quam, vel sollicitudin ipsum. Morbi at dapibus leo. Morbi eget eleifend neque. Pellentesque non dolor turpis. In diam orci, sagittis vel facilisis ipsum eu, tempus et tellus. Class aptent taciti sociosqu ad ipsum litora torquent per conubia nostra, per inceptos himenaeos. Phasellus at pretium metus. Quisque accumsan lobortis scelerisque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nullam facilisis adipiscing ipsum tempus. Vestibulum quis dictum dui. Donec ligula dui, feugiat eget rutrum vitae, dapibus sit amet quam. Praesent ullamcorper condimentum est at ultricies. Ut eu nulla magna, euismod posuere.";
   
    var subText = "ipsum";
};
</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
while
function occurrences(string, substring) {

  var n = 0;
  var pos = 0;

  while (true) {
    pos = string.indexOf(substring, pos);
    if (pos != -1) {
      n++;
      pos += substring.length;
    } else {
      break;
    }
  }
  return (n);
}

var n = occurrences(text, subText);
pending…
Regex
function occurrences(string, search) {
  var m = string.match(new RegExp(search.toString().replace(/(?=[.\\+*?[^\]$(){}\|])/g, "\\"), "g"));
  return m ? m.length : 0;
}

var n = occurrences(text, subText);
pending…
function occurrences(string, substring) {

  var n = 0;
  var pos = 0;
  var l=substring.length;

  while (true) {
    pos = string.indexOf(substring, pos);
    if (pos != -1) {
      n++;
      pos += l;
    } else {
      break;
    }
  }
  return (n);
}

var n = occurrences(text, subText);
pending…
function occurrences(string, substring) {

  var n = 0;
  var pos = 0;
  var l=substring.length;

  while (true) {
    pos = string.indexOf(substring, pos);
    if (pos > -1) {
      n++;
      pos += l;
    } else {
      break;
    }
  }
  return (n);
}

var n = occurrences(text, subText);
pending…
a
function countString(str, search){
    var count=0;
    var index=str.indexOf(search);
    while(index!=-1){
        count++;
        index=str.indexOf(search,index+1);
    }
    return 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