Javascript数组中的未定义值是否使用任何内存或在for-in循环中进行迭代? [英] Do undefined values in Javascript arrays use any memory or get iterated over in a for-in loop?

查看:129
本文介绍了Javascript数组中的未定义值是否使用任何内存或在for-in循环中进行迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为游戏构建一个实体系统,基本上我不确定是否应该使用简单的对象(字典)或数组通过其ID存储实体/组件.

I'm building an Entity System for a game, and basically I'm not sure whether I should use simple objects (dictionaries) or arrays to to store the entities/components by their id.

我最大的问题是我不想要动态实体ID.如果id只是一个字符串(使用字典存储实体),则它将始终有效,并且我可以使用storage [id]来访问该实体.

My biggest issue is that I didn't want a dynamic entity id. If the id was just a string (using the dictionary to store entities), then it would always be valid and I could use storage[id] to get to that entity.

如果我使用数组,我认为,表示存储数组中 index 的实体的ID将会改变.考虑以下实体数组:

If I used arrays, I thought, the id's of entities, which would represent an index in the storage array, would change. Consider this array of entities:

[
    Entity,
    Entity, //This one is being removed.
    Entity
];

如果要删除该数组中的第二个实体,我认为访问第三个数组所需的ID必须更改为(现在已删除)第二个实体的ID(索引).那是因为我考虑过要删除splice() ing.

If I was to remove the second entity in that array, I thought that the id required to access the third array would have to change to the id (index) of the (now removed) second entity. That's because I thought about deleting in terms of splice()ing.

但是,我可以使用delete表达式将元素(实体)变成undefined!而且,如果确实Javascript中的数组实际上只是对象,并且对象在逻辑上具有无限多个未定义值,这是否意味着数组内的未定义值不会耗尽内存?

But, I could use the delete expression to turn the element (an entity) into an undefined! And, if it's true that arrays in Javascript are actually just objects, and objects logically have infinitely many undefined values, does that mean that undefined values inside arrays don't use up memory?

最初,我虽然将数组实现为在内存中对齐,并且索引只是与第一个元素的偏移量,但根据这种逻辑,我认为未定义的值至少会使用内存.指针(因为我,实际上,认为元素的指针是对齐的,而不是元素本身).

Initially, I though that arrays were implemented in a way that they were aligned in memory, and that an index was just an offset from the first element, and by this logic I thought that undefined values would use at least the memory of a pointer (because I, actually, thought that pointers to elements are aligned, not elements themselves).

因此,如果我在此数组中存储了10k +个实体,并delete将它们的一半存储,那么5k undefined的实体将完全不使用任何内存吗?

So, if I stored 10k+ entities in this array, and deleteed half of them, would the 5k undefined's use any memory at all?

此外,当我执行for entity in array循环时,是否可以传递这些未定义的元素?

Also, when I do a for entity in array loop, would these undefined elements be passed?

此外,我在哪里可以找到资源来了解应如何在Java中实现实际上的数组?我所能找到的只是数组的一般说明以及如何使用它们的教程,但是我想了解所有这些在某些情况下可能很重要的小怪癖.像"JavaScript怪癖"网站之类的网站就很棒.

Also, where can I find resources to see how arrays are actually supposed to be implemented in Javascript? All I can find are general explanations of arrays and tutorials on how to use them, but I want to find out all about these little quirks that can prove important in certain situations. Something like a "Javascript quirks" site would be great.

推荐答案

数组不仅仅是对象.特别是length属性非常神奇.

Arrays are not just objects. In particular the length property is very magic.

当然,只要外部API保持不变,就可以允许JavaScript引擎以其选择的任何方式在内部表示数组.例如,如果您设置了随机分隔的值,则它们可能会存储为散列,但如果您设置了连续的值,则它们可能会被优化为数组.

Of course, a JavaScript engine is allowed to represent the array internally in any way it chooses, as long as the external API remains the same. For instance, if you set randomly separated values then they may be stored as a hash, but if you set consecutive values then they may be optimised into an array.

for ... in不会枚举未设置的属性.这包括跳过值的数组文字,例如[true, , false],它将仅枚举索引0和2.

for ... in does not enumerate properties that are not set. This includes an array literal that skips values e.g. [true, , false], which will only enumerate indices 0 and 2.

这篇关于Javascript数组中的未定义值是否使用任何内存或在for-in循环中进行迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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