"Type of undefined" vs "=== undefined" (v34)

Revision 34 of this benchmark created on


Preparation HTML

<script>
(function initMyTest(global) {
  var a = 1, b = true, c = 'c', d = [], e = {}, u, module = {};

  function typeOfStrict(v) {
    return typeof v === 'undefined';
  }

  function typeOfNonStrict(v) {
    return typeof v == 'undefined';
  }

  function globalUndefined(v) {
    return v === undefined;
  }

  function localUndefined(v) {
    return v === u;
  }

  function localUndefinedFn(v, u) {
    return v === u;
  }

  function void0(v) {
    return v === void 0;
  }

  function test(fn, args) {
    if (fn(a)) {
        throw new Error('');
    }
    if (fn(b)) {
        throw new Error('');
    }
    if (fn(c)) {
        throw new Error('');
    }
    if (fn(d)) {
        throw new Error('');
    }
    if (fn(e)) {
        throw new Error('');
    }
  }

  test(void0);
  test(typeOfStrict);
  test(typeOfNonStrict);
  test(globalUndefined);
  test(localUndefined);
  test(localUndefinedFn);

  module.test = test;
  module.void0 = void0;
  module.typeOfStrict = typeOfStrict;
  module.typeOfNonStrict = typeOfNonStrict;
  module.globalUndefined = globalUndefined;
  module.localUndefined = localUndefined;
  module.localUndefinedFn = localUndefinedFn;

  global.myTest = module;
}(window));
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
typeof strict
myTest.test(myTest.typeOfStrict);
 
ready
typeof non-strict
myTest.test(myTest.typeOfNonStrict);

 
ready
global undefined
myTest.test(myTest.globalUndefined);
 
ready
local undefined
myTest.test(myTest.localUndefined);
 
ready
local undefined (fn)
myTest.test(myTest.localUndefinedFn);
 
ready
void 0
myTest.test(myTest.void0);
 
ready

Revisions

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