Run details:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36
Chrome 79
Linux
Desktop
4 years ago
Test name Executions per second
regex 4949697.0 Ops/sec
split 6180327.5 Ops/sec
for loop1 10303806.0 Ops/sec
for loop 2 185075.1 Ops/sec
deleting 5308489.5 Ops/sec
histogram 68482.1 Ops/sec
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;
    }