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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser:
Chrome 131
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
Use regex
12649322.0 Ops/sec
Use split
30692560.0 Ops/sec
Use indexOf
23808120.0 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");