作者:手机用户2502876103 | 来源:互联网 | 2023-05-17 18:47
IwaswonderingabouthowObjectsareimplementedunderthehoodinJavascriptengines(V8,Spidermo
I was wondering about how Objects are implemented under the hood in Javascript engines (V8, Spidermonkey, etc). Are they really just Hash Tables? If so, how do they handle collisions?
我想知道如何在Javascript引擎(V8、Spidermonkey等)的框架下实现对象。它们真的只是哈希表吗?如果是,它们如何处理碰撞?
2 个解决方案
6
First of all, the answer is probably somewhat different for different JS engines. Also, I assume you're specifically asking about the property storage; obviously objects have a bunch of other state too (prototype chain link being an obvious one).
首先,对于不同的JS引擎,答案可能有些不同。另外,我猜你是在问关于财产储存的问题;显然,对象也有很多其他的状态(原型链链接是一个明显的状态)。
In the case of Spidermonkey, objects basically have a linked list of (propname, infromation about property) pairs, until they have too many properties, when I believe they still keep the linked list (because order matters for properties in JS in practice) but add an out-of-band hashtable that maps property names to entries in the linked list.
在Spidermonkey的情况下,对象主要有一个链表(propname,相关信息属性)对,直到他们有太多的属性,当我相信他们仍然把链表(因为订单事项属性JS在实践中)但添加一个带外散列表,属性名称映射到链表中的条目。
There may also be other reasons for the switch to the hashtable; the details haven't exactly been fixed over time and are likely subject to change in the future.
切换到hashtable可能还有其他原因;具体细节还没有随着时间的推移而确定,未来可能会发生变化。
The linked lists and hashtables are actually shared across objects; as long as two objects have the same property names and corresonding property information (which does NOT include the value, for properties with a stored value) and the properties were set in the same order, they're able to share the property linked list.
链接列表和散列表实际上在对象之间共享;只要两个对象具有相同的属性名和相关的属性信息(不包括值,对于具有存储值的属性),并且属性按照相同的顺序设置,它们就能够共享属性链接列表。
The actual property values, when those need to be stored, are stored in an array in the object (or more precisely, two arrays; one allocated inline with the object, whose size is fixed at object-creation time, one dynamically allocated and resized as needed for properties that are added later).
当需要存储这些属性值时,实际的属性值存储在对象中的数组中(或者更准确地说,是两个数组;一个是与对象内联分配的,对象的大小在创建对象时是固定的,另一个是动态分配的,并根据需要调整大小,以便稍后添加属性)。