Skip to content

Instantly share code, notes, and snippets.

@wan2land
Forked from lsauer/gist:2757250
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save wan2land/eecfcd9deb1ae3a539a1 to your computer and use it in GitHub Desktop.

Select an option

Save wan2land/eecfcd9deb1ae3a539a1 to your computer and use it in GitHub Desktop.

Revisions

  1. Changwan Jun revised this gist Jun 10, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    //참고 : http://jsperf.com/count-the-number-of-characters-in-a-string

    //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
  2. @lsauer lsauer revised this gist Nov 24, 2013. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@
    #3.
    var stringsearch = "o"
    ,str = "this is foo bar";
    for(var count=-1,index=0; index != -1; count++,index=str.indexOf(stringsearch,index+1) );
    for(var count=-1,index=-2; index != -1; count++,index=str.indexOf(stringsearch,index+1) );
    //>count:2
    #4.
    //searching for a single character
    @@ -83,4 +83,8 @@ var str = "this is foo bar",
    hist[str[si]] = hist[str[si]] ? 1+hist[str[si]]:1;
    }
    //>hist[schar]
    2
    2

    //Changelog > 11/2013:
    //
    // 24/11/2013 #3 bug fixed in initial index position; pointed out by Augustus@Stackoverflow
  3. @lsauer lsauer revised this gist Sep 3, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -46,7 +46,9 @@
    #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 ;};
    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;
  4. @lsauer lsauer revised this gist Sep 3, 2013. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -68,5 +68,17 @@
    //see: http://stackoverflow.com/questions/10293378/what-is-the-most-efficient-way-of-merging-1-2-and-7-8-into-1-7-2-8/17910641#17910641
    var str = "this is foo bar",
    schar = 'o';
    str.split('').reduce( function(p,c,i,a){ if(c === schar || p === schar){return isNaN(parseInt(p))? 1:+p+1;} return p;} )
    str.split('').reduce(
    function(p,c,i,a){ if(c === schar || p === schar){return isNaN(parseInt(p))? 1:+p+1;} return p;}
    )
    //Note: faster: c === schar || p === schar; slower: (c+p).indexOf(schar)>-1

    #10. dictionary character histogram
    var str = "this is foo bar",
    schar = 'o',
    hist={};
    for(si in str){
    hist[str[si]] = hist[str[si]] ? 1+hist[str[si]]:1;
    }
    //>hist[schar]
    2
  5. @lsauer lsauer revised this gist Sep 3, 2013. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -62,3 +62,11 @@
    for(var i=0;i<ubstr.length;schar!==ubstr[i++]||cnt++);
    //>cnt
    2

    #9
    //using reduce. Note: Element map functions are powerful but slow as they involve their own heap/stack allocation
    //see: http://stackoverflow.com/questions/10293378/what-is-the-most-efficient-way-of-merging-1-2-and-7-8-into-1-7-2-8/17910641#17910641
    var str = "this is foo bar",
    schar = 'o';
    str.split('').reduce( function(p,c,i,a){ if(c === schar || p === schar){return isNaN(parseInt(p))? 1:+p+1;} return p;} )
    //Note: faster: c === schar || p === schar; slower: (c+p).indexOf(schar)>-1
  6. @lsauer lsauer revised this gist May 29, 2012. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -52,4 +52,13 @@
    ,cnt=0;
    for(var i=0;i<bstr.byteLength;schar!==bstr[i++]||cnt++);
    //>cnt
    2
    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
  7. @lsauer lsauer revised this gist May 29, 2012. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -42,3 +42,14 @@
    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. @lsauer lsauer revised this gist May 28, 2012. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -35,4 +35,10 @@
    .filter(Boolean)
    //>[9, 10]
    [9, 10].length
    //>2
    //>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
  9. @lsauer lsauer revised this gist May 28, 2012. No changes.
  10. @lsauer lsauer revised this gist May 28, 2012. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -25,3 +25,14 @@
    ,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
  11. @lsauer lsauer revised this gist May 20, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -13,12 +13,12 @@
    //You can actually easily observe the EXACT resource usage using **Chrome's profiler** option.


    //#3.
    #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.
    #4.
    //searching for a single character

    var stringsearch = "o"
  12. @lsauer lsauer created this gist May 20, 2012.
    27 changes: 27 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    //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