Object is undefined

JavaScript performance comparison

Test case created by Corban Brook

Info

There are a few different ways to check if an object is undefined. Which is the best for speed and also size.

Preparation code

<script>
  var a;
  var b = 42;
  var undefinedVar;
  var undefinedStringCached = "undefined";
</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 "undefined"
if (typeof a === "undefined") {}
if (typeof b === "undefined") {}
pending…
typeof "undefined" cached
if (typeof a === undefinedStringCached) {}
if (typeof b === undefinedStringCached) {}
pending…
undefined
if (a === undefined) {}
if (b === undefined) {}
pending…
void(0)
if (a === void(0)) {}
if (b === void(0)) {}
pending…
undefined var
if (a === undefinedVar) {}
if (b === undefinedVar) {}
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