Skip to content

Instantly share code, notes, and snippets.

@caridy
Created November 21, 2012 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caridy/4126067 to your computer and use it in GitHub Desktop.
Save caridy/4126067 to your computer and use it in GitHub Desktop.
Compare set to null vs delete member in NodeJS
NodeJS 0.8.x on MBP:
$ node delete.js
89300 'value to null'
3100674 'deleting member'
var microtime = require('microtime'),
i,
t,
obj,
max = 10000000;
function run1(obj) {
obj.foo = null; // seeting value to falsy
t = microtime.now();
for (i = 0; i < max; i += 1) {
if (obj.foo) {
obj.foo();
}
}
console.log(microtime.now() - t, 'value to null');
}
function run2(obj) {
delete obj.foo; // deleting member
t = microtime.now();
for (i = 0; i < max; i += 1) {
if (obj.foo) {
obj.foo();
}
}
console.log(microtime.now() - t, 'deleting member');
}
run1({
foo: function () {
var f = 1;
},
bar: 1
});
run2({
foo: function () {
var f = 1;
},
bar: 1
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment