Using a dictionary instead of an object?
See original GitHub issueThe following easily fails:
const tlru = require('tiny-lru');
const cache = tlru();
cache.get('toString');
and this is a common issue when objects, instead of dictionaries, are used in conjunction of the in
operator.
In NodeJS, they’ve benchmarked that the best way to go is:
function Dict() {}
Dict.prototype = Object.create(null);
Once you have that, instead of this.items = {};
you’d go this.items = new Dict;
and you grant no name clashing through the prototype could possibly ever happen.
Thoughts?
Issue Analytics
- State:
- Created 4 years ago
- Comments:23 (14 by maintainers)
Top Results From Across the Web
python - Dictionary vs Object - which is more efficient and why?
It's only downside compared with dictionaries is that (like tuples) it doesn't give you the ability to change attributes after creation.
Read more >Dictionaries v. Objects - Matthew Rocklin
Shannon Behrens recently published a brief post on the use of dictionaries and objects to store named data. He raised the following question ......
Read more >What is the difference between a dictionary and an object in ...
Objects are more general than dictionaries. Objects can store local variables of any kind inside them, as well as define local methods/functions (local...
Read more >Python Dictionary as Object - Joel McCune
Starting simply enough, the above dictionary is converted into a nested object. Then, accessing properties is as simple as using dot syntax. ...
Read more >Using dictionaries to store data as key-value pairs
Using dictionaries to store data as key-value pairs. The dictionary stores objects as key-value pairs and can be used to represent complex real-world...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Just to be clear:
in
operator, because it reveals prototype chain detailsitems.hasOwnProperty
would be a bad choice, 'causehasOwnProperty
could be used as cache, so that havinghasOwnProperty.call(items, key)
would be at least saferObject.create(null)
to exactly address all these common hidden caveats with objects literals (and thanks gosh it works on prototype too)I’ve been working and contributing in Open Source for 20 years, and just landing PRs is not usually welcome, because devs might have reasons to pick one approach instead of another.
And yet, you are blaming me for opening a bug and discussing a possible solution before opening a PR … so yes, you’ve been a dick (since you mentioned that part).
Best Regards.
I’m also gonna lock this; anyone can open a pr if they want to land a change.