JavaScript and the `delete` keyword
You learn something new everyday. Today I learned that the statement object.property = null
is significantly more performant than delete object.property
in all modern browsers. Even though usingĀ delete
feels safer because the property is totally gone and thus can’t be used unintentionally (say, with a loop of Object.keys
or _.each(object, ...)
), definitely consider instead using checks like if (typeof object === 'object' && !!object.property) {}
if you’re into performance, or even just to be safer. On the other hand, the performance gains you get will probably only make a dent during a loop of hundreds or thousands of these operations. As with everything, this recipe calls for a grain of salt.
Comments