delete versus undefined

I’ve notice some calls to “delete” sprinkled throughout the code and I am wondering why we would prefer delete versus simply setting to undefined? This jsperf test shows undefined is faster by far:
http://jsperf.com/delete-vs-undefined-vs-null/6

Also, delete (by design) does not always succeed, it returns true or false because on whether the property was deleted or not. In the case of assigning undefined, the value will always end up undefined. For prototype based objects, I’m pretty sure delete kills performance as well.

This article seems to go pretty in-depth, but I admit I haven’t read the entire thing yet: http://perfectionkills.com/understanding-delete/

Is there something I’m missing here?

In V8, deleting properties will switch that object into dictionary mode, which as you mention is slow. In some cases like cache objects using arbitrary keys, it’s probably already in dictionary mode anyway, so deleting properties results in less memory usage. Also, delete won’t fail in any normal use case that I can think of.

In general, though, I’d say you’re right, prefer assigning undefined to delete, unless the set of properties you will be adding/removing are potentially unbounded and you’re concerned about long-lived objects with lots of leftover unused properties.

Sounds like the tip is worth adding to the conventions: https://github.com/AnalyticalGraphicsInc/cesium/wiki/JavaScript-Coding-Conventions.

Have at it.

Patrick