Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save paulstatezny/a2d2150a6743787ce96decca4c7c40af to your computer and use it in GitHub Desktop.
Save paulstatezny/a2d2150a6743787ce96decca4c7c40af to your computer and use it in GitHub Desktop.
Benchmarking how much faster it is to null a property of a complex object versus deleting that property in JavaScript
var Benchmark = require('benchmark');
function complete() {
console.log(String(this));
}
var objDeleteBenchmark = new Benchmark('Object#delete-property', {
setup: function() {
var obj = {
foo: {
bar: {
baz: 'baz',
qux: 'qux',
bool: false
}
},
foo2: {
bar2: {
baz: false
}
}
};
},
fn: function() { delete obj.foo.bar; }
});
var nullPropertyBenchmark = new Benchmark('Object#null-property', {
setup: function() {
var obj = {
foo: {
bar: {
baz: 'baz',
qux: 'qux',
bool: false
}
},
foo2: {
bar2: {
baz: false
}
}
};
},
fn: function() { obj.foo.bar = null; }
});
objDeleteBenchmark.run({ 'async': true }).on('complete', complete);
nullPropertyBenchmark.run({ 'async': true }).on('complete', complete);
@paulstatezny
Copy link
Author

$ node index.js
Object#null-property x 782,807,915 ops/sec ±1.17% (89 runs sampled)
Object#delete-property x 15,986,585 ops/sec ±0.96% (94 runs sampled)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment