delete vs undefined vs null

JavaScript performance comparison

Revision 13 of this test case created by Ian Nartowicz

Preparation code

<script>
  'use strict';
  var template = {
    a: 1,
    b: 2,
    c: 3,
    d: 4,
    e: 5,
    f: 6,
    g: 7,
    h: 8,
    i: 9,
    j: '10',
    k: 11,
    l: '12',
    m: function() {
      return 13;
    },
    n: 14,
    o: function() {
      return '15';
    }
  };
  var dump, log = function() {
      dump = arguments;
      };
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var o1 = {};
  for (var x in template) {
    o1[x] = template[x];
  }
  
  var o2 = {};
  for (var x in template) {
    o2[x] = template[x];
  }
  
  var o3 = {};
  for (var x in template) {
    o3[x] = template[x];
  }
  
  var o4 = {};
  for (var x in template) {
    o4[x] = template[x];
  }
  
  var o5 = {};
  for (var x in template) {
    if (!(x in ["a", "i", "d", "f"])) o5[x] = template[x];
  }
  
  o2.a = null;
  o2.i = null;
  o2.d = null;
  o2.f = null;
  
  o3.a = undefined;
  o3.i = undefined;
  o3.d = undefined;
  o3.f = undefined;
  
  delete o4.a;
  delete o4.i;
  delete o4.d;
  delete o4.f;
  

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Baseline (no modification)
log(o1.j + o1.l + o1.o(), o1.m());
pending…
Setting properties to null
log(o2.j + o2.l + o2.o(), o2.m());
pending…
Setting properties to undefined
log(o3.j + o3.l + o3.o(), o3.m());
pending…
Deleting properties
log(o4.j + o4.l + o4.o(), o4.m());
pending…
Never add properties
log(o5.j + o5.l + o5.o(), o5.m());
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.

0 Comments