fastest method to check if type is a number

JavaScript performance comparison

Test case created by Brian Jemilo II

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  a = '-1',
  b = '0',
  c = '1',
  d = '1.1',
  e = '5',
  f = 'A';

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
isNaN
isNaN(a);
isNaN(b);
isNaN(c);
isNaN(d);
isNaN(e);
isNaN(f);
pending…
isNaN(parseFloat)
isNaN(parseFloat(a));
isNaN(parseFloat(b));
isNaN(parseFloat(c));
isNaN(parseFloat(d));
isNaN(parseFloat(e));
isNaN(parseFloat(f));
pending…
typeof
typeof a === 'number';
typeof b === 'number';
typeof c === 'number';
typeof d === 'number';
typeof e === 'number';
typeof f === 'number';
pending…
parseInt
parseInt(a) || parseInt(a) === 0;
parseInt(b) || parseInt(b) === 0;
parseInt(c) || parseInt(c) === 0;
parseInt(d) || parseInt(d) === 0;
parseInt(e) || parseInt(e) === 0;
parseInt(f) || parseInt(f) === 0;
pending…
parseFloat
parseFloat(a) || parseFloat(a) === 0;
parseFloat(b) || parseFloat(b) === 0;
parseFloat(c) || parseFloat(c) === 0;
parseFloat(d) || parseFloat(d) === 0;
parseFloat(e) || parseFloat(e) === 0;
parseFloat(f) || parseFloat(f) === 0;
pending…
!isNaN
!isNaN(a);
!isNaN(b);
!isNaN(c);
!isNaN(d);
!isNaN(e);
!isNaN(f);
pending…
!isNaN(parseFloat)
!isNaN(parseFloat(a));
!isNaN(parseFloat(b));
!isNaN(parseFloat(c));
!isNaN(parseFloat(d));
!isNaN(parseFloat(e));
!isNaN(parseFloat(f));
pending…
typeof parseFloat
typeof parseFloat(a) === 'number';
typeof parseFloat(b) === 'number';
typeof parseFloat(c) === 'number';
typeof parseFloat(d) === 'number';
typeof parseFloat(e) === 'number';
typeof parseFloat(f) === 'number';
pending…

Compare results of other browsers

Revisions

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

0 Comments