Tests:
  • regex

    AخA
     
    ("this is foo bar".match(/o/g)||[]).length
  • split

     
    "this is foo bar".split("o").length-1
  • for loop1

     
    var stringsearch = "o";
    var str = "this is foo bar";
    for(var count=-1,index=-2; index != -1; count++,index=str.indexOf(stringsearch,index+1) );
  • for loop 2

     
    var stringsearch = "o";
    var str = "this is foo bar";
    for(var i=count=0; i<str.length; count+=+(stringsearch===str[i++]));
  • deleting

     
    var str = "this is foo bar";
    str.length - str.replace(/o/g,'').length;
  • histogram

     
    var str   = "this is foo bar";
    var schar = 'o';
    var hist={}; 
    for(si in str){
      hist[str[si]] = hist[str[si]] ? 1+hist[str[si]]:1;
    }
  • for in

     
    var str = "this is foo bar";
    var char = 'o';
    var count = 0;
    for (let c in str) {
      if (str[c] === char) count++;
    }
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    regex
    split
    for loop1
    for loop 2
    deleting
    histogram
    for in

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 5 years ago)
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36
Chrome 79 on Linux
View result in a separate tab
Test name Executions per second
regex 4693308.0 Ops/sec
split 6017223.0 Ops/sec
for loop1 11054253.0 Ops/sec
for loop 2 187614.0 Ops/sec
deleting 5546296.5 Ops/sec
histogram 68407.8 Ops/sec
for in 209206.4 Ops/sec