Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 2nd, 2012  |  syntax: None  |  size: 2.45 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //www.lsauer.com 2012
  2. //Answer to:
  3. //http://stackoverflow.com/questions/881085/count-the-number-of-occurances-of-a-character-in-a-string-in-javascript/10671743#10671743
  4. //There are at least four ways. The best option, which should also be the fastest -owing to the native RegEx engine -, is placed at the top. //jsperf.com is currently down, otherwise I would provide you with performance statistics.
  5.  
  6. #1.
  7.      ("this is foo bar".match(/o/g)||[]).length
  8.      //>2
  9. #2.
  10.     "this is foo bar".split("o").length-1
  11.      //>2
  12. //split is not recommended. Resource hungry. Allocates new instances of 'Array' for each match. Don't try that for a >100MB file via FileReader.
  13. //You can actually easily observe the EXACT resource usage using **Chrome's profiler** option.
  14.  
  15.  
  16. #3.
  17.     var stringsearch = "o"
  18.        ,str = "this is foo bar";
  19.     for(var count=-1,index=0; index != -1; count++,index=str.indexOf(stringsearch,index+1) );
  20.      //>count:2
  21. #4.
  22. //searching for a single character
  23.  
  24.     var stringsearch = "o"
  25.        ,str = "this is foo bar";
  26.     for(var i=count=0; i<str.length; count+=+(stringsearch===str[i++]));
  27.      //>count:2
  28.  
  29.  
  30. #5.
  31. //element mapping and filtering; not recommended due to its overall resource pre-allocation vs. Pythonian 'generators'
  32. //provides the position within the string
  33.     var str = "this is foo bar"
  34.     str.split('').map( function(e,i){ if(e === 'o') return i;} )
  35.              .filter(Boolean)
  36.     //>[9, 10]
  37.     [9, 10].length
  38.     //>2
  39.  
  40. #6
  41. //'deleting' the character out of the string and measuring the distance in length
  42.     var str = "this is foo bar";
  43.     str.length - str.replace(/o/g,'').length
  44.     //>2
  45.  
  46. #7
  47. //based on typed arrays; str2buffer is taken from 'is-lib'; See: https://gist.github.com/lsauer
  48.     //Converts an ASCII string to an typed-Array buffer
  49.     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 ;};
  50.     var bstr = str2buffer ("this is foo bar")
  51.        ,schar = 'o'.charCodeAt()
  52.        ,cnt=0;
  53.     for(var i=0;i<bstr.byteLength;schar!==bstr[i++]||cnt++);
  54.     //>cnt
  55.     2
  56. #8
  57. //based on untyped Arrays. Is expected to be slower. Analogous to #7
  58.     var ubstr = "this is foo bar".split('').map( function(e,i){ return e.charCodeAt();} )
  59.        //>[116, 104, 105, 115, 32, 105, 115, 32, 102, 111, 111, 32, 98, 97, 114]
  60.        ,schar = 'o'.charCodeAt()
  61.        ,cnt=0;
  62.     for(var i=0;i<ubstr.length;schar!==ubstr[i++]||cnt++);
  63.     //>cnt
  64.     2