Skip to content

Instantly share code, notes, and snippets.

@paulstatezny
Last active January 24, 2024 17:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulstatezny/a8bffd4d12074e3e70c4e2c7be96cd19 to your computer and use it in GitHub Desktop.
Save paulstatezny/a8bffd4d12074e3e70c4e2c7be96cd19 to your computer and use it in GitHub Desktop.
Benchmarking how much faster it is to null a property of an 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'}; },
fn: function() { delete obj.foo; }
});
var nullPropertyBenchmark = new Benchmark('Object#null-property', {
setup: function() { var obj = {foo: 'bar'}; },
fn: function() { obj.foo = null; }
});
objDeleteBenchmark.run({ 'async': true }).on('complete', complete);
nullPropertyBenchmark.run({ 'async': true }).on('complete', complete);
@paulstatezny
Copy link
Author

paulstatezny commented Apr 27, 2016

$ node index.js
Object#delete-property x 14,632,549 ops/sec ±2.00% (82 runs sampled)
Object#null-property x 1,069,853,337 ops/sec ±1.92% (85 runs sampled)

@paulstatezny
Copy link
Author

paulstatezny commented Apr 27, 2016

  • In this simplistic test, assigning to null is about 73 times faster than deleting a property.
  • In 1% of 1ms you can delete a property from an object 146 times.

Whether it's worth it to null out properties as an alternative to deletion is left as an exercise to the reader.

@paulstatezny
Copy link
Author

paulstatezny commented Apr 27, 2016

@winston0410
Copy link

Good job

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