Skip to content

Instantly share code, notes, and snippets.

@sairion
Last active May 5, 2017 17:29
Show Gist options
  • Save sairion/4ad6da9c21050af17974c4c6166e672b to your computer and use it in GitHub Desktop.
Save sairion/4ad6da9c21050af17974c4c6166e672b to your computer and use it in GitHub Desktop.
isdigit_bench.js
function isDigit_parse(num){ return !Number.isNaN(Number(num.trim())) } // should trim for same functionality
function isDigit_regex(num){ return rgx.test(num) }
var ts = 'a'
var emptyString = ''
var ts2 = 'b'
var rgx = /^[0-9]+$/
console.time('pI')
for (var i = 0; i > 100000000; i++) {
isDigit_parse(`${i}${i % 2 ? ts : emptyString}`)
}
console.timeEnd('pI')
console.time('reg')
for (var j = 0; j > 100000000; j++) {
isDigit_regex(`${j}${j % 2 ? ts2 : emptyString}`)
}
console.timeEnd('reg')
// in chrome 58, I get the result the regex function being about 1.3~2.0x faster, but it varies.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment