delete vs undefined vs null

JavaScript performance comparison

Revision 3 of this test case created

Info

The delete operator removes a property entirely. Setting a property to undefined removes the value. Setting a property to null changes the value to the null value.

Technically they are not equivalent, but in practice they are often used to mean the same thing: that a property is unset.

Preparation code

<script>
  o = {
   p: 1
  };
  var id = 0
 
  function reset() {
   o.p = ++id;
  }
</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
delete object.property
var i;
for (i = 0; i < 100; i++) {
 delete o.p;
 reset();
};
pending…
delete object["property"]
var i;
for (i = 0; i < 100; i++) {
 delete o["p"];
 reset();
}
pending…
object.property = undefined
var i;
for (i = 0; i < 100; i++) {
 o.p = undefined;
 reset();
}
pending…
object["property"] = undefined
var i;
for (i = 0; i < 100; i++) {
 o["p"] = undefined;
 reset();
}
pending…
object.property = null
var i;
for (i = 0; i < 100; i++) {
 o.p = null;
 reset();
}
pending…
object["property"] = null
var i;
for (i = 0; i < 100; i++) {
 o["p"] = null;
 reset();
}
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