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

Javascript 基础之 Map 和 WeakMap #4

Open
Ziphwy opened this issue Nov 3, 2020 · 0 comments
Open

Javascript 基础之 Map 和 WeakMap #4

Ziphwy opened this issue Nov 3, 2020 · 0 comments

Comments

@Ziphwy
Copy link
Owner

Ziphwy commented Nov 3, 2020

总结:

  1. Map 是有序键值对集合,Map 内部使用数组实现,读写时需要 O(n) 时间遍历 key 数组
  2. WeakMap 是解决 Map 使用 Object 作为 key 形成强引用,无其他引用也无法被 GC 的问题,因此只允许 Object 作为 key
  3. WeakMap 对 key 是弱引用,Object 在没有其他引用时就能被 GC
  4. 受 GC 的影响,WeakMap 无法返回确定性的 key,设计上不可枚举

MDN WeakMap 解释

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