Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Count string occurrence
http://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string http://stackoverflow.com/questions/881085/count-the-number-of-occurences-of-a-character-in-a-string-in-javascript
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0
Browser:
Firefox 121
Operating system:
Linux
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
Use regex
5834467.5 Ops/sec
Use split
7188058.5 Ops/sec
Use indexOf
8244290.5 Ops/sec
Script Preparation code:
function regex(s, re) { var r = s.match(re); if (r) return r.length; return 0; } function split(s, oc) { return s.split(oc).length - 1; } function indexOf(s, oc) { var i = 0, count = 0; while ((i = s.indexOf(oc, i)) >= 0) { count++; i++; } return count; }
Tests:
Use regex
window.result = regex("This is a pen", /is/g);
Use split
window.result = split("This is a pen", "is");
Use indexOf
window.result = indexOf("This is a pen", "is");