javascript中函数解析过程
a.b查找是有b, 先从Hidden Class查找(fast Path), 找到的话, 使用偏移来值, 找不到退化为(slow Path), 使用map查找
JSObject a
├─ Map* ──────────────► MapA
│ ├─ instance_size
│ ├─ descriptor_array
│ │ ├─ x @ offset 0
│ │ └─ y @ offset 1
│ └─ transition
│ └─ "z" → MapB
│
└─ properties[0] = 1
properties[1] = 2
Map 是“结构描述”
properties 是“值数组”
从map中拿到offset, 然后再拿值
fast Path:
map = a->map();
desc = map->descriptor_array["b"];
offset = desc->field_index();
value = a->properties[offset];
slow Path:
dict = a->property_dictionary();
entry = dict->Find("b"); // hash
|
对比项
|
数组
|
链表
|
Hidden Class
|
|---|---|---|---|
|
连续内存
|
✅
|
❌
|
✅(descriptor)
|
|
动态增长
|
❌
|
✅
|
✅(transition)
|
|
查找速度
|
O(1)
|
O(n)
|
O(1)
|
|
结构变化
|
❌
|
✅
|
✅
|
|
是否存值
|
✅
|
✅
|
❌
|
