如果v8在对象增长时进行反转 [英] If v8 rehashes when an object grows
问题描述
var x = {a:1,b:2,c:3}
//理论上触发一个调整大小
xd = 4
xe = 5
xf = 6
在v8中,散列的底层数组看起来像这样
[1,3,2,null,null]
它最初创造了一些额外的空间。但这还不够,所以它不得不成长。有两种选择。
- 它会在原来的位置放置原来的值。
- 它增长和 rehashes ,将值移动到任意新的地方。 p>
//(1)1,3,2停留在
[1,3,2,6,4 ,5,null,null,null,null]
//(2)1,3,2被移动
[6,2,5,3,4,1,null,想知道v8在这种情况下做了什么。也想知道调整大小的启发式是什么(当它超过数量时它是否加倍数组大小)。 - 它增长和 rehashes ,将值移动到任意新的地方。 p>
V8引擎使用两种对象表示形式: b
$ b
快速模式对于属性访问通常要快得多 - 但需要知道对象的结构。
V8最初会尝试构建一个对象看起来称为隐藏类的模板。该对象将通过隐藏类转换,直到V8放弃并将该对象存储为缓慢属性。
我深入讨论了这个问题,其中包含 至于你的直接问题,对象会 快速分配这些属性分配(在每个这样的分配上)并迁移到不同的地图(根据需要复制内存)。 Say you have a change in an object that triggers a change in the size of the underlying array or data structure storing the hash values. Say the underlying array for the hash looked like this in v8 It created some extra space initially. But it wasn't quite enough so then it had to grow. There are two options. So it would look like: Wondering what v8 does in this situation. Also wondering what the heuristics are for the resizing (does it double the array size when it outgrows, etc.). The V8 engine uses two object representations: Fast mode is typically much faster for property access - but requires the object's structure to be known. V8 will initially try to construct a template of what the object looks like called a "Hidden Class". The object will transform through hidden classes until V8 will give up and store the object as a slow property. I go into this more in depth with the relevant code in "How does Bluebird's util.toFastProperties function make an object's properties "fast"?
". As for your direct question the object would "fast assign" on those property assignments (on each such assignment) and migrate to a different map (copying the memory as needed). 这篇关于如果v8在对象增长时进行反转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$
$
var x = { a: 1, b: 2, c: 3 }
// trigger a resize theoretically
x.d = 4
x.e = 5
x.f = 6
[ 1, 3, 2, null, null ]
// (1) 1, 3, 2 stay where they are
[ 1, 3, 2, 6, 4, 5, null, null, null, null ]
// (2) 1, 3, 2 are moved
[ 6, 2, 5, 3, 4, 1, null, null, null, null ]