parseInt vs regex - if string is an int

Benchmark created by Hristofor Lukanov on


Description

Which is the fastest way to determin if a string value is an integer.

Preparation HTML

<script>
  var nums = [ '0x2', '2324564', 'test', '2' ], i = 0, c = nums.length;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
parseInt
for( i = 0; i < c; i++ ) {
    if( parseInt(nums[i]).toString() === nums[i] ) {
        //...
    }
}
ready
regex
for( i = 0; i < c; i++ ) {
    if( /^0-9+$/.test(nums[i]) ) {
        //...
    }
}
ready

Revisions

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

  • Revision 1: published by Hristofor Lukanov on