Script Preparation code:
x
 
 var stringsearch = "o", str = "this is foo bar";
str2buffer = function(s){ var bu = new ArrayBuffer(s.length), aUint8 = new Uint8Array(bu );
                         for(var i=0; i<bu.byteLength; aUint8[i]=s.charCodeAt(i),i++);return aUint8;
                        };
var bstr = str2buffer ("this is foo bar")
,schar = 'o'.charCodeAt()
,cnt=0;
var ubstr = "this is foo bar".split('').map( function(e,i){ return e.charCodeAt();} )
       ,schar = 'o'.charCodeAt()
       ,cnt=0;
var hist = {};
var len = str.length;
function countChar (char, search) {
  
  let num = 0;
  let str = search;
  let pos = str.indexOf(char);
  
  while(pos > -1) {
    str = str.slice(pos + 1);
    pos = str.indexOf(char);
    num++;
  }
  return num;
}
function countChar2 (char, str) {
  
  let num = 0;
  let pos = str.indexOf(char);
  
  while(pos > -1) {
    pos = str.indexOf(char, pos + 1);
    num++;
  }
  return num;
}
Tests:
  • Match

     
    ("this is foo bar".match(/o/g)||[]).length;
  • Split

     
    "this is foo bar".split("o").length-1;
  • For loop - access with count

     
    for(var count=-1,index=-2; index != -1; count++,index=str.indexOf(stringsearch,index+1));
  • For loop - searching for a single character

     
    for(var i=count=0; i<str.length; count+=+(stringsearch===str[i++]));
  • element mapping and filtering with position

     
    str.split('').map( function(e,i){ if(e === 'o') return i;} ).filter(Boolean).length;
  • deleting the character out of the string and measuring the distance in length

     
    str.length - str.replace(/o/g,'').length;
  • based on typed arrays access with cnt

     
    for(var i=0;i<bstr.byteLength;schar!==bstr[i++]||cnt++);
  • based on untyped Arrays access with cnt

     
    for(var i=0;i<ubstr.length;schar!==ubstr[i++]||cnt++);
  • Using reduce

     
    str.split('').reduce(function(p,c,i,a){ if(c === schar || p === schar){return isNaN(parseInt(p))? 1:+p+1;} return p;}) 
  • Histogram dict

     
    for (si in str) {
      hist[str[si]] = hist[str[si]] ? 1+hist[str[si]]:1;
    }
  • split filtering with destructor

     
    [...str].filter(l => l === 'o').length;
  • Sanity check for loop

     
    for (let charIndex = 0; charIndex < len; ++charIndex) {
      if (str[charIndex] === ',') {
        count++;
      }
    }
  • Sanity check for loop with of

     
    for (const char of str) {
      if (char === 'o') {
        count++;
      }
    }
  • For loop indexOf slicing

     
    countChar(stringsearch, str);
  • For loop indexOf from position

     
    countChar2(stringsearch, str);
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Match
    Split
    For loop - access with count
    For loop - searching for a single character
    element mapping and filtering with position
    deleting the character out of the string and measuring the distance in length
    based on typed arrays access with cnt
    based on untyped Arrays access with cnt
    Using reduce
    Histogram dict
    split filtering with destructor
    Sanity check for loop
    Sanity check for loop with of
    For loop indexOf slicing
    For loop indexOf from position

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 2 years ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36
Chrome 102 on Windows
View result in a separate tab
Test name Executions per second
Match 16062125.0 Ops/sec
Split 14865070.0 Ops/sec
For loop - access with count 2858150.5 Ops/sec
For loop - searching for a single character 172816.8 Ops/sec
element mapping and filtering with position 2246621.0 Ops/sec
deleting the character out of the string and measuring the distance in length 5605556.0 Ops/sec
based on typed arrays access with cnt 349560.8 Ops/sec
based on untyped Arrays access with cnt 365906.3 Ops/sec
Using reduce 605461.7 Ops/sec
Histogram dict 72845.0 Ops/sec
split filtering with destructor 5634831.0 Ops/sec
Sanity check for loop 604556.2 Ops/sec
Sanity check for loop with of 1738920.4 Ops/sec
For loop indexOf slicing 5107123.5 Ops/sec
For loop indexOf from position 5594127.0 Ops/sec