delete vs undefined vs null (v6)

Revision 6 of this benchmark created on


Description

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 HTML

<script>
  var id, o = {
    p: 100
  }, a = [];

for( var i = 0; i < 100; i++)
{
   a.push(i);
}

  function reset() {
    if (!o.p) o.p = ++id;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
delete object.property
id = 0;
for (var i = 0; i < 100; i++) {
  delete o.p;
  reset();
};
ready
delete object["property"]
id = 0;
for (var i = 0; i < 100; i++) {
  delete o["p"];
  reset();
}
ready
object.property = undefined
id = 0;
for (var i = 0; i < 100; i++) {
  o.p = undefined;
  reset();
}
ready
object["property"] = undefined
id = 0;
for (var i = 0; i < 100; i++) {
  o["p"] = undefined;
  reset();
}
ready
object.property = null
id = 0;
for (var i = 0; i < 100; i++) {
  o.p = null;
  reset();
}
ready
object["property"] = null
id = 0;
for (var i = 0; i < 100; i++) {
  o["p"] = null;
  reset();
}
ready
array delete
for( var i = 0; i < 100; i++ )
{
   delete a[i];
}
ready
array set to null
for( var i = 0; i < 100; i++ )
{
   a[i] = null;
}
ready
array set to undefined
for( var i = 0; i < 100; i++ )
{
   a[i] = undefined;
}
ready

Revisions

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