Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
count number of chars in a string 2
(version: 0)
Comparing performance of:
regex vs split vs for loop1 vs for loop 2 vs deleting vs histogram vs for in
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
regex
("this is foo bar".match(/o/g)||[]).length
split
"this is foo bar".split("o").length-1
for loop1
var stringsearch = "o"; var str = "this is foo bar"; for(var count=-1,index=-2; index != -1; count++,index=str.indexOf(stringsearch,index+1) );
for loop 2
var stringsearch = "o"; var str = "this is foo bar"; for(var i=count=0; i<str.length; count+=+(stringsearch===str[i++]));
deleting
var str = "this is foo bar"; str.length - str.replace(/o/g,'').length;
histogram
var str = "this is foo bar"; var schar = 'o'; var hist={}; for(si in str){ hist[str[si]] = hist[str[si]] ? 1+hist[str[si]]:1; }
for in
var str = "this is foo bar"; var char = 'o'; var count = 0; for (let c in str) { if (str[c] === char) count++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (7)
Previous results
Fork
Test case name
Result
regex
split
for loop1
for loop 2
deleting
histogram
for in
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Related benchmarks:
Count string occurrence
hamming distance.
hamming distance again
Count char occurrence in string
Testing character counting
Comments
Confirm delete:
Do you really want to delete benchmark?