V8 中新分配的 JavaScript 对象看起来像 (->
意思是“指向”):
[ class ] -> ... ; pointer to the hidden class
[ properties ] -> [empty array]
[ elements ] -> [empty array] ; elements are properties with numeric names
[ reserved #1 ] -\
[ reserved #2 ] |
[ reserved #3 ] }- space reserved for "in object properties"
............... |
[ reserved #N ] -/
每个对象中都有一定的空间预先分配给所谓的对象属性。 V8 根据构造函数选择预分配属性的数量(例如,以下形式的赋值数量)this.field = expr
)和运行时分析。
当您向对象添加新属性时,V8 首先尝试放入预先分配的对象内槽中。当对象内槽耗尽时,V8 开始将它们放入对象外属性数组中。属性名称和属性索引之间的映射存储在隐藏类中。例如 JS 对象{ a: 1, b: 2, c: 3, d: 4}
might看起来像:
[ class ] -> [a: in obj #1, b: in obj #2, c: out obj #1, d: out obj #2]
[ properties ] -> [ 3 ][ 4 ] ; this is linear array
[ elements ]
[ 1 ]
[ 2 ]
If properties
数组变得太大 V8 会正常化一个对象:将其属性转换为字典形式:
[ class ] -> [ OBJECT IS IN DICTIONARY MODE ]
[ properties ] -> [a: 1, b: 2, c: 3, d: 4, e: 5] ; this is classical hash table
[ elements ]