Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map/Set #148

Closed
azu opened this issue Oct 2, 2016 · 1 comment
Closed

Map/Set #148

azu opened this issue Oct 2, 2016 · 1 comment

Comments

@azu
Copy link
Collaborator

azu commented Oct 2, 2016

#17 基本文法

データ構造の例としてMap/Set/WeakMap/WeakSetについてを扱う。
やれば大体ArrayとObjectでできてしまうけど、他の言語だと結構こういうデータ構造は色々持っているものも多い。
(なので、JavaScriptを長くやっている人ほどArrayとかで頑張るもしない気もしないでもない)

ObjectとMapの具体的な違いとかに着目した例を出したい気がする。
MapはObjectをkeyにできるけど、ObjectのKeyはstringになるなど。

The keys of an Object are Strings and Symbols, where they can be any value for a Map.
-- Map - JavaScript | MDN

具体的にMapをES5レベルで実装すると結構面倒くさい。
keyにobjectを使えると、キャッシュするときにkeyがstringに限定されなくて便利とか使い道は結構ある。
LRUの実装とかはなかなか良さそうな感じ。

WeakはES5レベルでは実装できないし、JavaScriptはGCな言語なのでメモリリークを無くすには必要。
WeakはあるなしでAPIのデザインが異なると思う。(自動解放で済むならAPIがシンプルになる)

また、JavaScript特有?なのかはわからないけど、Weakは基本的に列挙できないAPIデザインになってる。

Note that neither type of weak collection is iterable. You can’t get entries out of a weak collection except by asking for them specifically, passing in the key you’re interested in.
-- ES6 In Depth: Collections ★ Mozilla Hacks – the Web developer blog

これは初期のドラフトだとforEachとかそういうものは合ったけど、ない方が効率いい実装ができるということで列挙系が落ちた。

こういう違いについてもサラッと触れられるといい気がする。

ユースケースはWikiに色々乗ってる

実際使わなくてもプログラミングはできるけど、使うことでより効率的な実装ができたり、データ構造をイメージするものなので追加要素的な立ち位置ではあると思う。

@azu
Copy link
Collaborator Author

azu commented May 26, 2017

#238 へ移動

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant