JavaScript 数组具有关联性吗?

发布于 2024-08-27 20:19:14 字数 60 浏览 5 评论 0原文

例如,如果我这样做 a[1000000]=1; 它会使用 1000000 个元素的内存还是仅用于这个元素?

For instance, if I do
a[1000000]=1;
will it use memory for 1000000 elements or just for this one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

手长情犹 2024-09-03 20:19:14

在 ECMAScript 标准(第 15.4 节)中,数组的唯一特殊之处是 length 属性会自动更新(以及一堆特定于数组的原型函数):

数组对象对某一类属性名称给予特殊处理。属性名称​​P(以字符串值的形式)是数组索引当且仅当ToString(ToUint32(P )) 等于 PToUint32(P)不等于 232−1。
...
每个 Array 对象都有一个 length 属性,其值始终是小于 232 的非负整数。 length 属性的值在数值上大于每个名称为数组索引的属性的名称; ...

除此之外,数组只是一个对象,这意味着它可以被视为关联数组, 尽管你不应该


如今,JS 引擎应该检测数组是密集还是非常稀疏,并在内部使用线性数组或关联数组之间进行切换。在你的例子中,JS 引擎不会分配一百万个元素。

In the ECMAScript standard (§15.4), the only special thing about array is that the length property is automatically updated (and a bunch of Array-specific prototype functions):

Array objects give special treatment to a certain class of property names. A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 232−1.
...
Every Array object has a length property whose value is always a nonnegative integer less than 232. The value of the length property is numerically greater than the name of every property whose name is an array index; ...

Other than that, an Array is just an Object, which means it can be treated as an associative array, although you shouldn't.


Nowadays the JS engines should detect whether the array is dense or very sparse and switch between using a linear or associative array internally. In your case, the JS engine won't allocate a million elements.

稳稳的幸福 2024-09-03 20:19:14

会创建 1,000,000 个元素吗?

不会,数组是稀疏的,但它们的索引将是持久的。 编辑:实际上,它们的稀疏性是特定于实现的,但是在 a[1000000] = 1 的情况下保持它们稀疏对我来说似乎是合乎逻辑的事情。

var a = [1, 2, 3, 4];
var x = a[1]; // -> x := 2

delete a[1];
var y = a[1]; // -> y := undefined

a[9] = 10;
var y = a[8]; // -> z := undefined

JS 数组是关联数组吗?

JavaScript 数组是关联数组的子集(索引必须是整数,如 KennyTM 的答案。JavaScript 对象是完全关联的:

var o = { "key1": "value1", "key2": "value2" };
var i = "key2";
var v = o[i]; // -> v := "value2"

Would 1,000,000 elements be created?

No, arrays are sparse, but their index will be persistent. EDIT: Actually, their sparseness would be implementation-specific, but keeping them sparse in case of a[1000000] = 1 would seem a logical thing to me.

var a = [1, 2, 3, 4];
var x = a[1]; // -> x := 2

delete a[1];
var y = a[1]; // -> y := undefined

a[9] = 10;
var y = a[8]; // -> z := undefined

Are JS arrays associative?

JavaScript arrays are a subset of associative arrays (in that indices have to be integers, as shown in KennyTM's answer. JavaScript objects are fully associative:

var o = { "key1": "value1", "key2": "value2" };
var i = "key2";
var v = o[i]; // -> v := "value2"
人疚 2024-09-03 20:19:14

在某些情况下,您可以使用对象文字作为一种“关联数组”:

var object = {
  "first": "1",
  "second": "2",
  "third": "3",
  "fourth": "4"
};
object.fifth = "5";
object.["sixth"] = "6";

但它有其局限性......没有神奇的“长度”参数,并且您将无法访问每个数组都有的方法。

You may use object literal as a kind of 'associative aray' in some cases:

var object = {
  "first": "1",
  "second": "2",
  "third": "3",
  "fourth": "4"
};
object.fifth = "5";
object.["sixth"] = "6";

But it has its limitations... There is no magic 'length' parameter and you will not have access to methods that every array has.

北座城市 2024-09-03 20:19:14

JS 数组是自动增长的。在空数组上将 a[100] 设置为 1 将会用“未定义”填充前 99 个元素。

JS arrays are auto-growing. Setting a[100] to 1 on an empty array will populate the first 99 elements with "undefined".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文