function countCharacter_reduce(str, ch) {
return Array.prototype.reduce.call(str, (prev, cur) => cur === ch && ++prev && prev, 0);
}
for(let i = 0; i < 10000; i++) {
countCharacter_reduce('this/is/a/path/with/extension', '/' )
}
function countCharacter_split(str, ch) {
return str.split(ch).length - 1;
}
for(let i = 0; i < 10000; i++) {
countCharacter_split('this/is/a/path/with/extension', '/' )
}
function countCharacter_for(str, ch) {
for (var count = 0, ii = 0; ii < str.length; ii++) {
if (str[ii] === ch)
count++;
}
return count;
}
for(let i = 0; i < 10000; i++) {
countCharacter_for('this/is/a/path/with/extension', '/' )
}
function countCharacter_regex(str, ch) {
return str.length - str.replace(new RegExp(ch, 'g'), '').length;
}
for(let i = 0; i < 10000; i++) {
countCharacter_regex('this/is/a/path/with/extension', '/' )
}
function countCharacter_indexOf(str, char) {
var start = 0;
var count = 0;
while ((start = str.indexOf(char, start) + 1) !== 0) {
count++;
}
return count;
}
for(let i = 0; i < 10000; i++) {
countCharacter_indexOf('this/is/a/path/with/extension', '/' )
}