在这种情况下,数组大小是否在javascript中很重要?

时间:2022-02-02 01:39:29

I'm creating an array in javascript using item id's from the database whenever a corresponding button is clicked on the page. Each array entry will store a custom object.

每当在页面上单击相应的按钮时,我将使用数据库中的项目ID在javascript中创建一个数组。每个数组条目都将存储一个自定义对象。

The id's from the database for a specific can start from any number like 80123 to 80223, for that specific page of data.

对于特定的数据页面,特定数据库中的id可以从80123到80223之间的任何数字开始。

so the first entry in the array will be like arr[80123].

所以数组中的第一个条目就像arr [80123]。

Now when i check the length of the array it shows me 80123 ! even though theres only 1 element in it, i thought of using associative or character indexed arrays, but they lack some basic sorting operations that i would need.

现在,当我检查阵列的长度时,它显示我80123!即使它只有1个元素,我想到使用关联或字符索引数组,但它们缺少一些我需要的基本排序操作。

Now my question is "How much memory will the array actually be consuming if there is only 1 element in it but the length of the array is 80123 ?"

现在我的问题是“如果数组中只有1个元素,但数组的长度是80123,那么数组实际消耗的内存量是多少?”

More info...

The base number keeps changing 80123 is only an example.

基数不断变化80123只是一个例子。

the code im using is as follows:

我使用的代码如下:

function ToggleAction(actionButtonID, action) 
    {
        var extractedID = ExtractNumericIdFromTag(actionButtonID);
        var arrayIndexer = extractedID; // Can update this to make array associative

        if(actionItems[arrayIndexer] == null)
        {
            actionItems[arrayIndexer] 
                = new ActionItem(extractedID, action);
        }
        else
        {
            var ai = actionItems[arrayIndexer];
            ai.action = action;
        }
    }

6 个解决方案

#1


6  

Your code is not allocating that much memory, so you don't have to worry about that. Javascript Arrays are smart enough. However, I still think you should be using an Object instead of an Array in this case...

您的代码没有分配那么多内存,因此您不必担心这一点。 Javascript Arrays非常聪明。但是,在这种情况下,我仍然认为你应该使用Object而不是Array ...

var num = 80123;
var obj = {};
obj[num] = { Name: "Josh" };

alert(obj[80123].Name);

#2


3  

Arrays in Javascript are just hashes with a few special properties (mainly that the "length" property will always return one higher than the highest integer key).

Javascript中的数组只是具有一些特殊属性的哈希值(主要是“length”属性将始终返回高于最高整数键的值)。

I don't believe that any of the implementations would allocate a huge amount of memory just because you initially assigned something to a really high index.

我不相信任何实现会分配大量内存只是因为你最初分配了一些非常高的索引。

#3


1  

Try running the following code in Firebug:

尝试在Firebug中运行以下代码:

var a = [];
a[663] = "a";
a.length == 664;

this returns

true

If you then try

如果你再试试

console.log(a)

you will get:

你会得到:

[undefined, undefined, undefined,  ...... , undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, "a"]

How much memory this consumes may be a matter of which engine you're using but it looks like there are a lot of slots being allocated to hold no value.

这消耗了多少内存可能是你正在使用哪个引擎的问题,但看起来有很多插槽被分配来保持没有价值。

I think what you really want to use is just a plain Object-style map like this:

我认为你真正想要使用的只是一个简单的对象风格的地图,如下所示:

var a = {};
a[664] = "a";
a[323] = "b";
a

which yields:

Object 664=a 323=b

which is much more appropriate for mapping associations of id->object.

这更适合映射id-> object的关联。

If you need to iterate through this object later on to visit all the objects, use the following code:

如果以后需要遍历此对象以访问所有对象,请使用以下代码:

for(var id in a){
  if(a.hasOwnProperty(id)){
    console.log("id:",id," object:",a[id]);
  }
}

#4


0  

How are you defining your array? If you use the Array object, you must have more than one argument for the browser to interpret the arguments as values and not the length of the array. I always try to use the literal array syntax: a = [80123]; or a = [80123,80124];

你是如何定义阵列的?如果使用Array对象,则浏览器必须具有多个参数才能将参数解释为值,而不是数组的长度。我总是尝试使用文字数组语法:a = [80123];或者a = [80123,80124];

#5


0  

This question has already been hinted around before:

这个问题之前已经被暗示过了:

Which takes less memory: a Javascript array or Javascript object?

这需要更少的内存:Javascript数组或Javascript对象?

Theoretically, in Javascript, an Array IS an Object. The only thing that is changing (in memory) in an array is the .length property. It will show the next highest array index, but it doesn't mean that there are actually that many elements in the Array allocated.

从理论上讲,在Javascript中,Array是一个Object。在数组中唯一改变(在内存中)的是.length属性。它将显示下一个最高的数组索引,但这并不意味着实际上分配了数组中的许多元素。

#6


0  

I'm not sure how js arrays behave, but i think you are safe here.

我不确定js数组的行为,但我认为你在这里很安全。

Just in case you are not, I think you can consider changing the index of array based on the minimum value of id (80123 in your example), so that the array index starts at zero.

如果你不是,我认为你可以考虑根据id的最小值(在你的例子中为80123)更改数组的索引,以便数组索引从零开始。

index = id - minID;

This might require changes in code in (many) other places too, so go for it only if it's absolutely necessary.

这可能需要在(很多)其他地方更改代码,所以只有在绝对必要的情况下才能使用它。

#1


6  

Your code is not allocating that much memory, so you don't have to worry about that. Javascript Arrays are smart enough. However, I still think you should be using an Object instead of an Array in this case...

您的代码没有分配那么多内存,因此您不必担心这一点。 Javascript Arrays非常聪明。但是,在这种情况下,我仍然认为你应该使用Object而不是Array ...

var num = 80123;
var obj = {};
obj[num] = { Name: "Josh" };

alert(obj[80123].Name);

#2


3  

Arrays in Javascript are just hashes with a few special properties (mainly that the "length" property will always return one higher than the highest integer key).

Javascript中的数组只是具有一些特殊属性的哈希值(主要是“length”属性将始终返回高于最高整数键的值)。

I don't believe that any of the implementations would allocate a huge amount of memory just because you initially assigned something to a really high index.

我不相信任何实现会分配大量内存只是因为你最初分配了一些非常高的索引。

#3


1  

Try running the following code in Firebug:

尝试在Firebug中运行以下代码:

var a = [];
a[663] = "a";
a.length == 664;

this returns

true

If you then try

如果你再试试

console.log(a)

you will get:

你会得到:

[undefined, undefined, undefined,  ...... , undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, "a"]

How much memory this consumes may be a matter of which engine you're using but it looks like there are a lot of slots being allocated to hold no value.

这消耗了多少内存可能是你正在使用哪个引擎的问题,但看起来有很多插槽被分配来保持没有价值。

I think what you really want to use is just a plain Object-style map like this:

我认为你真正想要使用的只是一个简单的对象风格的地图,如下所示:

var a = {};
a[664] = "a";
a[323] = "b";
a

which yields:

Object 664=a 323=b

which is much more appropriate for mapping associations of id->object.

这更适合映射id-> object的关联。

If you need to iterate through this object later on to visit all the objects, use the following code:

如果以后需要遍历此对象以访问所有对象,请使用以下代码:

for(var id in a){
  if(a.hasOwnProperty(id)){
    console.log("id:",id," object:",a[id]);
  }
}

#4


0  

How are you defining your array? If you use the Array object, you must have more than one argument for the browser to interpret the arguments as values and not the length of the array. I always try to use the literal array syntax: a = [80123]; or a = [80123,80124];

你是如何定义阵列的?如果使用Array对象,则浏览器必须具有多个参数才能将参数解释为值,而不是数组的长度。我总是尝试使用文字数组语法:a = [80123];或者a = [80123,80124];

#5


0  

This question has already been hinted around before:

这个问题之前已经被暗示过了:

Which takes less memory: a Javascript array or Javascript object?

这需要更少的内存:Javascript数组或Javascript对象?

Theoretically, in Javascript, an Array IS an Object. The only thing that is changing (in memory) in an array is the .length property. It will show the next highest array index, but it doesn't mean that there are actually that many elements in the Array allocated.

从理论上讲,在Javascript中,Array是一个Object。在数组中唯一改变(在内存中)的是.length属性。它将显示下一个最高的数组索引,但这并不意味着实际上分配了数组中的许多元素。

#6


0  

I'm not sure how js arrays behave, but i think you are safe here.

我不确定js数组的行为,但我认为你在这里很安全。

Just in case you are not, I think you can consider changing the index of array based on the minimum value of id (80123 in your example), so that the array index starts at zero.

如果你不是,我认为你可以考虑根据id的最小值(在你的例子中为80123)更改数组的索引,以便数组索引从零开始。

index = id - minID;

This might require changes in code in (many) other places too, so go for it only if it's absolutely necessary.

这可能需要在(很多)其他地方更改代码,所以只有在绝对必要的情况下才能使用它。