Last active
January 24, 2024 17:36
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
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.
Good job
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment