obscure typeof vs straightforward typeof - false test

Benchmark created by T.J. Crowder on


Description

Tests the difference in performance between the obscure ({"function":1}[typeof somefunc]) way of testing if something is a function vs the straightforward (typeof somefunc=="function"). Using the version with quotes to work around a bug in IE.

This is the test for when the result is false.

Setup

function somefunc() {}
    var functionTester = {"function":1};

Test runner

Ready to run.

Testing in
TestOps/sec
Obscure inline
if ({"function":1}[typeof foo]) throw "Error";
ready
Obscure cached
if (functionTester[typeof foo]) throw "Error";
ready
Straightforward
if (typeof foo=="function") throw "Error";
ready

Revisions

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

  • Revision 1: published by T.J. Crowder on