regex vs. split Return substring instance count (v7)

Revision 7 of this benchmark created by Austin Pray on


Description

Return substring instance count

Preparation HTML

<script>
  function solution(fullText, searchText){
    return fullText.split(searchText).length - 1;
  }
  
  function solution2(fullText, searchText){
    return (fullText.match(RegExp(searchText, "g")) || []).length;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
split
solution('abcdeb','b');
solution('abcdeb', 'a');
solution('ccddeeccddeecc', 'cc');
solution('aabb', 'cc');
solution('aabb', 'cc');
ready
regex
solution2('abcdeb','b');
solution2('abcdeb', 'a');
solution2('ccddeeccddeecc', 'cc');
solution2('aabb', 'cc');
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.