Now I'm going through my scripts once more to improve them, I find myself sometimes faced with indecision when it comes to property vs. key. And I wish there could be more guidance in the docs along the lines of: "for these usage scenarios, looking up a Map's keys is more appropriate/efficient over accessing an Object's properties (or vice versa)"
For example, say you have an HTTP server and when you handle a request, should you use Map or (mere) Object for the queries? For example say this is the type of URL you expect to handle
https://www.wikidata.org/w/api.php?action=wbgetclaims&entity=Q16850826&property=P31
Should you have request.queries["action"] or request.queries.action? Should you make "action" one of the keys in the queries Map, or should you make it one of the properties of the queries Object? *Philosophically*, I'm inclined to go with the latter approach, since I'm inclined to say that the queries in a http request are separate components of the request, much like the in representing e.g. a mouse position, x and y are separate components of the position. Clearly the more appropriate choice in the mouse position case would be position.x and position.y rather than position["x"] and position["y"]. Likewise for URL queries.
But in addition to philosophical considerations there are also technical ones. For example, other things being equal, are there any performance overhead when doing Map lookup (A["B"]) than when accessing the value of a property (A.B)? It seems that in theory the latter should be more efficient, since Map lookup necessarily involves looking up a string at (post-autoexec) runtime, whereas an Object's properties are like variable names (with the same restrictions, e.g. you can't have Object.<+j but you can have Map["<+j"]) , so there are optimization that can be done when the script is first loaded?
It'd be great if the doc could offer some guidance on this front. In moving from v1 to v2, this is one of the additional choice points one must often face when writing a script. I appreciate the additional rigor (my v2 scripts have much fewer runtime errors than my v1 scripts), but I wonder if the doc could offer a little more guidance.
(This post is inspired by seeing this comment from RaptorX, who says he's defaulting on Map most of the time. I had to do the same for the initial conversion, but I'd much rather know when to use which than to just use Map all the time
)RaptorX wrote: ↑17 Sep 2022, 21:36I very seldom use literal objects since I switched to v2, I use maps most of the times because it avoids using invalid names as properties.
I wish the object literal syntax was used for maps if the property names were quoted but Lexikos is against the idea or is not interested in implementing it as far as I remember.