test if number

JavaScript performance comparison

Test case created by Adam Krebs

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var num = '5'
    var string = 'nope'
    var mixed1 = '6seven'
    var mixed2 = 'eight9ten'
};
</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
regular expression
var digitFormatter = function (val) {
  if (!/^\d+$/.test(val)) { return true }
};

digitFormatter(num);
digitFormatter(string);
digitFormatter(mixed1);
digitFormatter(mixed2);
pending…
Unary operator
var digitFormatter = function(val) {
  var digit = +val;
  if (!isNaN(digit)) { return true }
};

digitFormatter(num);
digitFormatter(string);
digitFormatter(mixed1);
digitFormatter(mixed2);
pending…
ParseInt
var digitFormatter = function(val) {
  var digit = parseInt(val, 10);
  if (!isNaN(digit)) { return true }
};

digitFormatter(num);
digitFormatter(string);
digitFormatter(mixed1);
digitFormatter(mixed2);
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