Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Count string period occurrence
(version: 1)
Comparing performance of:
Use regex vs Use split with regex vs Use forloopset vs forloop2
Created:
11 months ago
by:
Guest
Jump to the latest result
Script Preparation code:
function regex(s) { var r = s.match(/[..。。]/g); if (r) return r.length; return 0; } function split(s) { return s.split(/[..。。]/).length - 1; } function forloopset(s, oc) { let c = 0; for (let i =0; i < s.length; i++){ if (oc.has(s[i])) c++} return c; } function forloop(s) { let c = 0; for (let i =0; i < s.length; i++){ if (s[i] ==="."|| s[i] === "."|| s[i] === "。"|| s[i] === "。") c++} return c; }
Tests:
Use regex
regex("This is a pen. This is a pen。 Another one.");
Use split with regex
split("This is a pen. This is a pen。 Another one.");
Use forloopset
forloopset("This is a pen. This is a pen。 Another one.", new Set([".",".","。","。"]));
forloop2
forloop("This is a pen. This is a pen。 Another one.");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Use regex
Use split with regex
Use forloopset
forloop2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Use regex
8797397.0 Ops/sec
Use split with regex
7699239.5 Ops/sec
Use forloopset
2251794.2 Ops/sec
forloop2
11083674.0 Ops/sec
Related benchmarks:
Count string occurrence
Split strings by character count
Split strings by character count (regex vs. slice vs. substr)
Subsequence by string manipulation or regex
testquery
test regex vs loop
Count char occurrence in string
Trimming leading/trailing string of characters
new Url vs host RegEx vs extractDomain lib
Comments
Confirm delete:
Do you really want to delete benchmark?