如果我只在数组中设置一个高索引,它会浪费内存吗? [英] If I set only a high index in an array, does it waste memory?

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

问题描述

在 Javascript 中,如果我做类似的事情

In Javascript, if I do something like

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

这会以某种方式浪费内存吗?我记得读过一些关于 Javascript 数组仍然为未指定的索引设置值的内容(也许将它们设置为未定义?),但我认为这可能与删除有关.我真的不记得了.

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.

推荐答案

查看此主题: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天全站免登陆