在引擎盖下,JavaScript对象的哈希表? [英] Under the hood, are Javascript objects hash tables?

查看:156
本文介绍了在引擎盖下,JavaScript对象的哈希表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道如何在Javascript引擎(V8,Spidermonkey等)下实现对象。他们真的只是哈希表?如果是的话,他们是如何处理冲突的?

解决方案

首先,不同的JS引擎可能会有所不同。另外,我假设你正在具体询问物业储存。显然对象也有一堆其他的状态(原型链链接是一个明显的)。

在Spidermonkey的情况下,对象基本上有一个链接列表(propname, infromation关于财产)对,直到他们有太多的属性,当我相信他们仍然保持链接列表(因为订单在JS实践中的属性很重要),但添加一个带外哈希表,映射属性名称的条目链接列表。



切换到哈希表可能还有其他原因。这些细节并没有被完全固定,并且很可能在将来发生变化。

链接列表和哈希表实际上是跨对象共享的;只要两个对象具有相同的属性名称和相关的属性信息(对于具有存储值的属性不包含该值)并且属性按相同顺序设置,则它们能够共享属性链接列表。当需要存储的实际属性值存储在对象的数组中时(或者更准确地说,是两个数组;一个与对象内联分配的数组,其大小固定在对象创建时间,动态分配和调整大小以适应稍后添加的属性。)


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?

解决方案

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).

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.

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.

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).

这篇关于在引擎盖下,JavaScript对象的哈希表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆