如果我在一个阵列只设置了高指标,它浪费内存? [英] If I set only a high index in an array, does it waste memory?

查看:137
本文介绍了如果我在一个阵列只设置了高指标,它浪费内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Javascript中,如果我做这样的事情。

In Javascript, if I do something like

var alpha = [];
alpha[1000000] = 2;

这是否浪费内存不知何故?我记得读一些关于JavaScript数组仍然不明指标设定值(也许它们设置为undefined),但我认为这可能已经是与删除。我真的不记得。

does this waste memory somehow? I remember reading something about Javascript arrays still setting values for unspecified indices (maybe sets them to undefined?), but I think this may have had something to do with delete. I can't really remember.

推荐答案

请参见本主题:
<一href=\"http://stackoverflow.com/questions/1510778/are-javascript-arrays-sparse\">are-javascript-arrays-sparse

在Javascript中的大多数实现(可能是所有现代的)数组是稀疏。这意味着,没有,它不会分配内存达到最大的索引。

In most implementations of Javascript (probably all modern ones) arrays are sparse. That means no, it's not going to allocate memory up to the maximum index.

如果它像一个Lua实现任何居然有一个内部数组和字典。从起始索引人口稠密地区将存储阵列中,稀疏部分在字典中

If it's anything like a Lua implementation there is actually an internal array and dictionary. Densely populated parts from the starting index will be stored in the array, sparse portions in the dictionary.

这篇关于如果我在一个阵列只设置了高指标,它浪费内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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