added this optimization:
How to count string occurrence in string?
This is probably the fastest implementation here, but it would be even faster if you replaced "++pos" with "pos+=searchFor.length" –hanshenrik
function occurrences(str_, subStr) { let occurence_count = 0 let pos = -subStr.length while ((pos = str_.indexOf(subStr, pos + subStr.length)) > -1) { occurence_count++ } return occurence_count}