Count integers in string (v2)

Revision 2 of this benchmark created on


Preparation HTML

<script>
  var str = '1a2b3c'
    , count = 0
    , re = /\d/g;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
regex match
count = (str.match(re) || []).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
regex match 2
count = (str.match(re) || '').length
ready

Revisions

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