有人可以帮助描述Javascript中的两种类型的数组存储吗? [英] Could someone help describe the two types of array storage in Javascript?
问题描述
我正在 HTML5Rocks 上阅读有关V8的这篇文章。这篇文章很老了,但我几乎一无所知,这让我很烦恼。我一次采取这一步,但有人可以帮助我使用数组部分吗?
I'm reading this article about V8 on HTML5Rocks. The article is old but I understand almost none of it and it bothers me. I'm taking this 1 step at a time but could someone help me with the Arrays section?
文章指出:
数组
为了处理大型和稀疏数组,有两种类型的$ b内部$ b数组存储:
In order to handle large and sparse arrays, there are two types of array storage internally:
-
快速元素:紧凑型密钥集的线性存储
Fast Elements: linear storage for compact key sets
字典元素:哈希表存储,否则
最好不要让数组存储从一种类型翻转到
另一种类型。
It's best not to cause the array storage to flip from one type to another.
问题:
快速元素线性存储阵列会是什么样的?
What would a Fast Elements linear storage array look like?
词典元素哈希表数组会是什么样子?
What would a Dictionary Elements hash table array look like?
出于预防目的,我将如何从一种类型转换到另一种类型?
For prevention purposes, how would I "flip from one type to another"?
推荐答案
我会走另一条路。
2)字典元素
2) What would Dictionary Elements
hash table Array look like?
JavaScript对象是从字符串到值的映射。例如
A JavaScript object is a map from string to values. e.g.
var obj = {
"name": "Sherlock Holmes",
"address": "221B Baker Street"
}
V8使用哈希表来表示对象,除非使用优化特殊情况的表示。这很像字典使用(单词,含义)对。
V8 uses hash tables to represent objects unless using an optimized representation for special cases. This is much like a dictionary uses (words, meaning) pair.
现在,这个哈希表访问速度很慢,因为最初哈希表中的所有键和值都是未定义
。在插入新对时,计算散列并在插入索引处插入对。如果该索引处已有一个键,则尝试在下一个键处插入,依此类推。
Now, this hash table access is slow because initially all the keys and values in a hash table are undefined
. On inserting a new pair, a hash is computed and the pair inserted at the insertion index. If there's already a key at that index, attempt to insert at next one and so on.
1)快速元素
线性存储阵列是什么样的?
1) What would Fast Elements
Linear storage Array look like?
在V8中,元素
是属性,其键是是一个非负整数(0,1,2,...),即一个简单的线性数组,其属性可以通过数字索引访问。
In V8, an element
is a property whose key is a non-negative integer (0, 1, 2, ...) i.e. a simple linear array whose properties can be accessed via a numerical index.
快速元素存储在连续的数组中。例如。
Fast elements are stored in a contiguous array. e.g.
var arr = [1, 2, 3];
它们是特殊情况,经过优化,可以更快地作为索引访问已知并且无法计算。
They are a special case that is optimised for faster access as the index is already known and not to be computed.
3)出于预防目的,我如何从一种类型翻转到另一种类型
?
3) For prevention purposes, How would I flip from one type to another
?
对于快速元素,如果指定的索引超出了元素数组的末尾,则V8可能会将元素降级为字典模式。
For fast element, if you assign an index that's way past the end of the elements array, V8 may downgrade the elements to dictionary mode.
参考: http ://jayconrod.com/posts/52/a-tour-of-v8-object-representation
这篇关于有人可以帮助描述Javascript中的两种类型的数组存储吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!