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 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Mobile Safari/537.36
Browser:
Chrome Mobile 119
Operating system:
Android
Device Platform:
Mobile
Date tested:
2 years ago
Test name
Executions per second
Use regex
1826098.6 Ops/sec
Use split
2500027.2 Ops/sec
Use indexOf
2443515.8 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");