question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using a dictionary instead of an object?

See original GitHub issue

The 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:closed
  • Created 4 years ago
  • Comments:23 (14 by maintainers)

github_iconTop GitHub Comments

2reactions
WebReflectioncommented, Sep 13, 2019

Just to be clear:

  • using an object literal as trash bin has never been a good idea in general, that is why Map was introduced in the language
  • assuming you can be careless and use the object literal, you should not use the in operator, because it reveals prototype chain details
  • even using items.hasOwnProperty would be a bad choice, 'cause hasOwnProperty could be used as cache, so that having hasOwnProperty.call(items, key) would be at least safer
  • because of all these gotchas described in previous points, they introduced Object.create(null) to exactly address all these common hidden caveats with objects literals (and thanks gosh it works on prototype too)
  • as result, you are using the wrong primitive to obtain unsafe, easy to break, results

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.

0reactions
avoidworkcommented, Sep 14, 2019

I’m also gonna lock this; anyone can open a pr if they want to land a change.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found