The purpose of the map
function is to create an associative-map object whose set of properties can be quickly altered.
The natural question arrises: aren't all JavaScript objects already maps by default? Yes, they are! The EMCAScript specification allows objects to add or drop properties at any time, allowing them to function as associative maps.
But, alas, the low-level language that is responsible for implementing the JavaScript execution environment (likely C++) is not so easygoing. In particular, V8 uses a concept called hidden classes, whereby the addition of a property to a JavaScript object will cause the creation of a new C++ class. V8 does this as an optimization because it assumes your code will repeatedly use a small set of object types.
For example, you have a Bullet
type with x
, y
, dx
, and dy
properties. In practical terms, these types are fixed; it's not likely that you would suddenly add on a new property to a Bullet
object on the fly. The hidden-class optimization means that using a fixed set of object types runs very quickly, but it also means that, sometimes, the real cost of adding a new property to a JS object can be quite high, because it prompts the creation of a new C++ class that has the new property.
By introducing a delete
operation on the object x
, you signal to the V8 engine that this object x
will not benefit from the hidden-class optimization. The idea behind hidden classes is that your objects will not usually change their set of properties (except adding new properties at construction time). By doing a delete
you unequivocally signal that this object will change its property set in ways that make hidden classes totally unhelpful. For this object, the cost of creating hidden classes far outweighs the benefits.
Thus, the object returned by map
will be excluded from V8 hidden-class optimizations, allowing it to add and remove arbitrary properties much more quickly.