Javascript数组稀疏吗? [英] Are Javascript arrays sparse?

查看:131
本文介绍了Javascript数组稀疏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也就是说,如果我使用当前时间作为数组的索引:

That is, if I use the current time as an index into the array:

array[Date.getTime()] = value;

解释器会实例化从0到现在的所有元素吗?不同的浏览器是否采用不同的方式?

will the interpreter instantiate all the elements from 0 to now? Do different browsers do it differently?

我记得曾经是 AIX 内核,它会根据请求创建伪ttys,但如果你这样做,比如echo> / dev / pty10000000000它会创建/ dev / pty0,/ dev / pty1,....然后摔倒了。在贸易展览会上这很有趣,但我不希望这发生在我的客户身上。

I remember there used to be a bug in the AIX kernel, which would create pseudo-ttys on request, but if you did, say, "echo > /dev/pty10000000000" it would create /dev/pty0, /dev/pty1, .... and then fall over dead. It was fun at trade shows, but I don't want this to happen to my customers.

推荐答案

JavaScript数组到底是多少实现与浏览器不同,但它们通常会回退到稀疏实现 - 很可能与常规对象的属性访问相同 - 如果使用实际数组效率低下。

How exactly JavaScript arrays are implemented differs from browser to browser, but they generally fall back to a sparse implementation - most likely the same one used for property access of regular objects - if using an actual array would be inefficient.

你必须要求对具体实现有更多了解的人来回答激烈触发从密集到稀疏的转变,但你的例子应该是完全安全的。如果你想获得一个密集的数组,你应该使用一个显式长度参数调用构造函数,并希望你实际上得到一个。

You'll have to ask someone with more knowledge about specific implementations to answer what excatly triggers the shift from dense to sparse, but your example should be perfectly safe. If you want to get a dense array, you should call the constructor with an explicit length argument and hope you'll actually get one.

参见这个答案有关olliej的详细说明。

See this answer for a more detailed description by olliej.

这篇关于Javascript数组稀疏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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