Count integers in string

Benchmark created by Ricardo on


Preparation HTML

<script>
  var str = '1a2b3c'
    , count = 0
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
regex match
count = (str.match(/\d/g) || []).length
ready
RegExp
var r = new RegExp('\\d', 'g')

while(r.exec(str)) count++
ready
Convert to number (?)
for (i in str){
    if (!isNaN(parseInt(str[i]))){
        count++
    }
}
ready
Replace
count = str.replace(/\D/g, '').length
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.