Empty vs. null vs. undefined (v3)

Revision 3 of this benchmark created by Milan Adamovsky on


Description

Want to see what is the best way to pass an empty parameter.

Setup

var myNull = null,
        myUndef = undefined,
        myEmpty = "";

Test runner

Ready to run.

Testing in
TestOps/sec
null
(function (x)
 {
  var a;

  if (x === null)
   {
    a = new Date();
   }

  return a;
 })(null);
ready
empty
(function (x)
 {
  var a;

  if (x === "")
   {
    a = new Date();
   }

  return a;
 })("");
ready
undefined
(function (x)
 {
  var a;

  if (x === undefined)
   {
    a = new Date();
   }

  return a;
 })(undefined);
ready
myNull
(function (x)
 {
  var a;

  if (x === myNull)
   {
    a = new Date();
   }

  return a;
 })(null);
ready
0
(function (x)
 {
  var a;
  if (x === 0)
   {
    a = new Date();
   }

  return a;
 })(0);
ready
false
(function (x)
 {
  var a;
  if (x === false)
   {
    a = new Date();
   }

  return a;
 })(false);
ready
true
(function (x)
 {
  var a;
  if (x === true)
   {
    a = new Date();
   }

  return a;
 })(true);
ready
1
(function (x)
 {
  var a;
  if (x === 1)
   {
    a = new Date();
   }

  return a;
 })(1);
ready
Double negate
(function (x)
 {
  var a;
  if (x === !!0)
   {
    a = new Date();
   }

  return a;
 })(!!0);
ready

Revisions

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

  • Revision 1: published by Milan Adamovsky on
  • Revision 3: published by Milan Adamovsky on