Empty vs. null vs. undefined

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 = "error";

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

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

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

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

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

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

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

  return a;
 })(null);
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