Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a property of an object inside its definition; Why?

Looking at the recent Google Maps API loader source, I'm wondering what is the purpose of the following:

google.maps.Load = function(apiLoad) {
    delete google.maps.Load;
    ...

Why would you delete a property of an object, inside its definition? I suspect it could have some performance increase, but can't figure out how a property can delete itself inside its definition.

like image 470
Mahdi Avatar asked Dec 24 '22 15:12

Mahdi


1 Answers

Obviously we can only make assumptions since it's only that code author can say for sure.

If the reason was to ensure that the Load procedure is performed just once then the decision chosen is really poor.

The problem is that deletion of properties makes impossible V8 (and may be other engines) to use so called "hidden classes" (which is an optimisation method for faster object's properties lookup).

The better alternative would be

google.maps.Load = function() {};

or

google.maps.Load = function() { throw new Error("Already loaded") };

as suggested by @Sam in the comments.

References:

  • Understanding hidden classes in v8
  • Fast Property Access
like image 87
zerkms Avatar answered Dec 28 '22 10:12

zerkms