笔记:V8 的 Javascript 对象示意

内容为该问题下的答案:http://segmentfault.com/q/1010000002423380

是对 http://jayconrod.com/posts/52/a-tour-of-v8-object-representation 的学习笔记。

《笔记:V8 的 Javascript 对象示意》

对象的两层实现

两层实现分别为:

  • Named properties: in-object properties and extra properties
  • Numbered properties: fast elements

Named properties

in-object properties 运用大众 map 来形貌其结构,某些情况(难以共用 map)会降级为 Dictionary mode。

V8 can handle minor divergences like this just fine, but if your code assigns all sorts of random properties to objects from the same constructor in no particular order, or if you delete properties, V8 will drop the object into dictionary mode, where properties are stored in a hash table. This prevents an absurd number of maps from being allocated.

一个叫做 In-object slack tracking 的过程用来决定对象的大小,其后的属性作为 extra properties 运用单独的数组储存。

Now I’m sure your next question is, “what happens when a new property is added after in-object slack tracking is complete?” This is handled by allocating an overflow array to store the extra properties. The overflow array can always be reallocated with a larger size as new properties are added.

最后,methods 和别的属性一道,储存在 map 里。

Numbered properties

个中,fast elements 包含

  • fast small integers
  • fast doubles
  • fast values

但是 fast elements 有时也会降级为 dictionary mode

If you assign to an index that’s way past the end of your the elements array, V8 may downgrade the elements to dictionary mode.

当然由于分开储存,降级并不会影响到别的类型的属性。

Because named properties and elements are stored separately, even if an object drops into dictionary mode for elements, named properties may still be accessed quickly (and vice versa).

    原文作者:bf
    原文地址: https://segmentfault.com/a/1190000002425695
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞