Triple equal to a typeof

JavaScript performance comparison

Test case created

Info

Is a triple equal better than a double equal to a result of a typeof? The result of a typeof is always a string, and as such there would be no need to use the triple equal comparison (which we use to avoid type coersion) unless it was a bit more performant. If it is not, then E.G. minifiers should change typeof comparisons to double equal.

Let's try this one out.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    this.aStr = 'this is a string!'
    this.aNumber = 65536
    this.anObject = {
      an: 'object'
    }
    this.aNull = null
    this.aFunction = function() {
      return 'hi!';
    }
};

Benchmark.prototype.teardown = function() {
    delete this.aStr
    delete this.aNumber
    delete this.anObject
    delete this.aNull
    delete this.aFunction
};
</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
triple equals
if (typeof this.aStr !== 'string') { throw 'Sanity check failed' }
typeof this.aStr === 'string'
typeof this.aNumber === 'number'
typeof this.anObject === 'object'
typeof this.aNull === 'object'
typeof this.aFunction === 'function'
pending…
double equals
if (typeof this.aStr != 'string') { throw 'Sanity check failed' }
typeof this.aStr == 'string'
typeof this.aNumber == 'number'
typeof this.anObject == 'object'
typeof this.aNull == 'object'
typeof this.aFunction == 'function'
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