typeof number vs unary plus

JavaScript performance comparison

Test case created by Jarrod Overson

Info

Typeof check when looking for number, vs unary +

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var toCheck = 42;
    var a = 0;
   
    var numString = 'number';
};
</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
typeof
if (typeof toCheck === 'number') {
 a++;
}
pending…
typeof cached comparison
if (typeof toCheck === numString) {
 a++;
}
pending…
unary +
if (toCheck === +toCheck) {
 a++;
}
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. Here’s a list of current revisions for this page:

0 comments

Add a comment