undefined vs local undefined var

JavaScript performance comparison

Test case created

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
global undefined
var stuff = 1;

if (stuff === undefined) {
  stuff += stuff; // never reached
local undefined var
var stuff = 1, undefined;

if (stuff === undefined) {
  stuff += stuff; // never reached

Compare results of other browsers


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:


Thomas Fuchs commented :

Note that this is mostly a size optimization, so closures can be minified better (variable renaming/shortening).

Tyler Washburn commented :

I love that the major of the test results are from iPhones.

John-David Dalton commented :

Libs usually add undefined as an argument in their IIFE:

;(function(window, undefined) {
// lib...

This also avoids edge issues of undefined being overwritten in environments where it's writable.

There's no common-use perf concern either. Note that even the slowest is still several millions of ops/sec on mobile/old-IE.

Minification is tricky and best left to minifiers. I've noticed several times, that adding variables in an attempt to reduce code size actually increased the gzipped size.

In my current project I run my code through several minifiers (and option combinations) to pick the smallest gzipped result (I automate this as part of the build process).

Comment form temporarily disabled.

Add a comment