Counting Occurrences of Char in Javascript

Comments

A JSPerf test to decide the best approach for counting occurrences of a character in a string. “string”.split( char ) seems the way to go.

“string”.split( RegExp ) with a RegExp as argument is consistently slower, which is what I expected (I hardly ever use it). Safari seems to have optimized regexp, but they are slower everywhere else. Manual loops and “string”.split( char ) seem faster across most platforms, with “string”.split( char ) being the most convenient to use.

Comments