- //www.lsauer.com 2012
- //Answer to:
- //http://stackoverflow.com/questions/881085/count-the-number-of-occurances-of-a-character-in-a-string-in-javascript/10671743#10671743
- //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.
- #1.
- ("this is foo bar".match(/o/g)||[]).length
- //>2
- #2.
- "this is foo bar".split("o").length-1
- //>2
- //split is not recommended. Resource hungry. Allocates new instances of 'Array' for each match. Don't try that for a >100MB file via FileReader.
- //You can actually easily observe the EXACT resource usage using **Chrome's profiler** option.
- #3.
- var stringsearch = "o"
- ,str = "this is foo bar";
- for(var count=-1,index=0; index != -1; count++,index=str.indexOf(stringsearch,index+1) );
- //>count:2
- #4.
- //searching for a single character
- var stringsearch = "o"
- ,str = "this is foo bar";
- for(var i=count=0; i<str.length; count+=+(stringsearch===str[i++]));
- //>count:2
- #5.
- //element mapping and filtering; not recommended due to its overall resource pre-allocation vs. Pythonian 'generators'
- //provides the position within the string
- var str = "this is foo bar"
- str.split('').map( function(e,i){ if(e === 'o') return i;} )
- .filter(Boolean)
- //>[9, 10]
- [9, 10].length
- //>2
- #6
- //'deleting' the character out of the string and measuring the distance in length
- var str = "this is foo bar";
- str.length - str.replace(/o/g,'').length
- //>2
- #7
- //based on typed arrays; str2buffer is taken from 'is-lib'; See: https://gist.github.com/lsauer
- //Converts an ASCII string to an typed-Array buffer
- 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;
- for(var i=0;i<bstr.byteLength;schar!==bstr[i++]||cnt++);
- //>cnt
- 2
- #8
- //based on untyped Arrays. Is expected to be slower. Analogous to #7
- var ubstr = "this is foo bar".split('').map( function(e,i){ return e.charCodeAt();} )
- //>[116, 104, 105, 115, 32, 105, 115, 32, 102, 111, 111, 32, 98, 97, 114]
- ,schar = 'o'.charCodeAt()
- ,cnt=0;
- for(var i=0;i<ubstr.length;schar!==ubstr[i++]||cnt++);
- //>cnt
- 2