hex check: parseInt vs regex

JavaScript performance comparison

Test case created by Adam Crabtree

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var validStr = '5daabfd355de86f925d6e716e9a41c570324b066922c23bf4bacbc857827c4c1'
    var invalidStr = '5daabfd355de86f925d6e716e9a41c570324b066922c23bf4bacbc857827c4cX'
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
parseInt - valid
var i = 0, substring, isValid = true

while (substring = validStr.substr(i, 12)) {
  if (parseInt(substring, 16).toString(16) !== substring) {
    isValid = false
    break
  }
  i += 12
}
pending…
parseInt - invalid
var i = 0, substring, isValid = true

while (substring = invalidStr.substr(i, 12)) {
  if (parseInt(substring, 16).toString(16) !== substring) {
    isValid = false
    break
  }
  i += 12
}
console.log(isValid)
pending…
regex - valid
new RegExp('[0-9a-f]{'+validStr.length+'}').test(validStr)
pending…
regex - invalid
new RegExp('[0-9a-f]{'+invalidStr.length+'}').test(invalidStr)
pending…

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

Compare results of other browsers

0 comments

Add a comment