浅析v8中weakSet和weakMap(待续)
来源:互联网 发布:java正则表达式匹配{} 编辑:程序博客网 时间:2024/06/06 01:44
图中为整个代码的执行过程。从js层初始化一个集合到底层的实现,其他的增删改查也类似的流程。底层是一个由UHashElement结构体组成的数组,UHashElement结构体包括每个元素的键值和哈希值。UHashtable中还含有一些扩大和缩小哈希表的操作。
set和map基本类似,下面通过代码大概了解一下。
function WeakMapConstructor(iterable) { // 必须加new if (IS_UNDEFINED(new.target)) { throw MakeTypeError(kConstructorNotFunction, "WeakMap"); } // 初始化底层哈希表 %WeakCollectionInitialize(this); // 如果没有传参则创建一个空的哈希表 if (!IS_NULL_OR_UNDEFINED(iterable)) { // set函数在下面代码中挂载 var adder = this.set; // 判断set是不是函数 if (!IS_CALLABLE(adder)) { throw MakeTypeError(kPropertyNotFunction, adder, 'set', this); } // 迭代参数,逐个加到哈希表中 for (var nextItem of iterable) { if (!IS_RECEIVER(nextItem)) { throw MakeTypeError(kIteratorValueNotAnObject, nextItem); } // 把内容加到哈希表 %_Call(adder, this, nextItem[0], nextItem[1]); } }}function WeakMapGet(key) { if (!IS_WEAKMAP(this)) { throw MakeTypeError(kIncompatibleMethodReceiver, 'WeakMap.prototype.get', this); } if (!IS_RECEIVER(key)) return UNDEFINED; // 根据键拿到一个hashcode,然后到哈希表里进行操作 var hash = GetExistingHash(key); if (IS_UNDEFINED(hash)) return UNDEFINED; return %WeakCollectionGet(this, key, hash);}
我们可以看到js层的代码很简单,我们new Map的时候创建一个哈希表,增删改查的时候,首先根据key拿到一个hashcode,然后到底层哈希表里进行操作。
时间有限,后面有时间继续研究,,,
阅读全文
0 0
- 浅析v8中weakSet和weakMap(待续)
- SE6新特性之集合Set、Map、WeakSet和WeakMap详解
- ES6中Set和WeakSet的使用
- ES6中Set和WeakSet的使用
- ES6中Set和WeakSet的使用
- es6 map和 weakmap
- es6 set和weakset
- WeakSet
- 浅析protobuf(未完待续)
- 从零开始学_JavaScript_系列(45)——ES6新增数据结构:Map和WeakMap
- 从零开始学_JavaScript_系列(44)——ES6新增数据结构:Set类型和WeakSet
- ES6--set集合和WeakSet集合
- es6学习笔记Set和WeakSet数据结构
- lua和V8 JavaScript初学中,,,,(二)
- freetype中FT_Set_Transform()和FT_Load_Char()分析(待续)
- Agile和scrum敏捷开发(未完待续中)
- ArcGIS安装中,问题和解决方法总结(待续)
- Java中HashMap和TreeMap的区别(未完待续)
- spark 批量写入redis (pipeline + 分批提交)
- 数学之美笔记3
- 112. Path Sum(DFS)
- Atitit 手机号码选号 规范 流程 attilax总结 v2 r99.docx
- Atititi. naming spec 联系人命名与remark备注指南规范v5 r99.docx
- 浅析v8中weakSet和weakMap(待续)
- 变量作用域
- 洛谷P1529 回家 Bessie Come Home
- Atitit 号码规范 靓号指南 attilax总结 v4 r926.docx
- Linux系统操作练习(一)
- atitit tag标签概念与原理 attilax总结 v2 r922.docx
- ubuntu16.04+cuda8+opencv3.1+zed sdk2.1+ros 安装配置
- atitit tag标签标示规范 attilax总结 v2 r922.docx
- 数据结构 --- Java之树的实现