JavaScript数组在物理内存中的表现如何? [英] How are JavaScript arrays represented in physical memory?

查看:167
本文介绍了JavaScript数组在物理内存中的表现如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,我可以将混合数据存储在JavaScript数组中,并将数组中的任何元素更改为其他类型。解释器如何跟踪物理内存中的任何元素的位置。另外,如果我将元素更改为更大的数据类型,那么如何覆盖下一个元素中的数据。



我假定数组只存储对实际对象的引用,并且原始图形被放置在数组中后面。



假设是这种情况,如果我在原始变量上有一个不同的句柄,并更改存储在数组是否保持同步?



我知道我可能已经回答了我自己的问题,但我不知道肯定,我找不到任何有关此事的信息。

解决方案

通常,数组分配一个连续的固定长度的内存块。然而,在Javascript中,数组是具有特殊构造函数和访问器方法的Object类型。



这意味着,如下所示:

  var arr = new Array(100000); 

不分配任何内存!实际上,它只是设置数组中length属性的值。构建数组时,您不需要自动声明一个大小。所以,你应该使用它:

  var arr = []; 

Javascript中的数组是稀疏的,这意味着数组中的所有元素都不能包含数据。换句话说,只有实际包含数据的元素存在于数组中。这减少了阵列使用的内存量。这些值由一个键而不是一个偏移量定位。它们只是一种方便的方法,而不是用于复杂的数值分析。



Javascript中的数组不会键入,因此元素的值可以是对象,字符串,数字,布尔,函数或数组。数组和对象之间的主要区别是length属性,其值大于数组中最大的整数键。



例如:



您可以创建一个空数组并添加索引0和索引99的两个元素。长度为100,但数组中的元素数将为2。

  arr [0] = 0; 
arr [99] = {name:John};
console.log(arr.length); //打印100
arr; //打印像[0,undefined×98,Object {name:John}]

直接回答您的问题:


Q。我的理解是,我可以将混合数据存储在JavaScript数组中,并将数组中的任何元素更改为其他类型。解释器如何跟踪物理内存中的任何元素的位置?此外,如果我将元素更改为更大的数据类型,那么如何覆盖下一个元素中的数据?



A。如果你已经阅读过我的评论,现在你可能已经知道了。在Javascript中,数组是一个Hashtable对象类型,因此解释器不需要跟踪物理内存,并且更改元素的值不会影响其他元素,因为它们不会存储在连续的内存块中。 / p>

-


Q。我假设数组只存储对实际对象的引用,并且在将数组放置在数组中之后,原语将被包装在幕后。假设是这种情况,如果我对原始变量有不同的句柄,并且更改存储在数组中的值是否保持同步性?



A。不,原语不包裹。更改分配给数组的原语将不会更改数组中按值存储的值。另一方面,对象通过引用存储,所以更改对象值将反映该数组中的更改。


这里是一个你可以尝试的例子:

  var arr = []; 
var obj = {name:John};
var isBool = true;

arr.push(obj);
arr [1] = isBool;

console.log(arr [0]); // print obj.name
console.log(arr [1]); // print true

obj.age = 40; //将age添加到obj
isBool = false; //更改isBool的值

console.log(arr [0]); //这里的值将包含age
console.log(arr [1]); //这里的值仍然是真的

另外,请注意,当您在以下两个中初始化一个数组时方法,它有一个不同的行为:

  var arr = new Array(100); 
console.log(arr.length); //打印100
console.log(arr); // prints []

var arr2 = new Array(100,200);
console.log(arr2.length); //打印2
console.log(arr2); //打印[100,200]

如果要使用Javascript数组作为连续的内存块,您应该查看使用 TypedArray 。 TypedArray允许您分配一个内存块作为字节数组,并更有效地访问原始二进制数据。



您可以通过阅读一个href =http://www.ecma-international.org/ecma-262/5.1/Ecma-262.pdf =nofollow noreferrer> ECMA-262规范(版本5.1)。 p>

It is my understanding that I can store mixed data in a JavaScript array, as well as change any element in the array to some other type. How does the interpreter keep track of what place in physical memory any element is at. Also how is the overwriting of the data in the next element prevented if I change an element to a larger data type.

I assume that arrays only store references to actual objects, and primitives are wrapped behind the scenes when placed in arrays.

Assuming this is the case, If I have a different handle on the primitive variable and change the value stored in the array is synchronicity maintained?

I know I probably already answered my own question, but I don't know for sure and I can't find any information on the matter.

解决方案

Normally, arrays allocate a contiguous block of memory of fixed length. However, in Javascript, arrays are Object types with special constructors and accessor methods.

Which means, a statement like:

var arr = new Array(100000);

does not allocate any memory! In fact, it simply sets the value of the length property in the array. When you construct an array, you don't need to declare a size as they grow automatically. So, you should use this instead:

var arr = [];

Arrays in Javascript are sparse which means not all the elements in the array may contain data. In other words, only the elements that actually contain data exist in the array. This reduces the amount of memory used by the array. The values are located by a key and not by an offset. They're simply a method of convenience and not intended to be used for complex numerical analysis.

Arrays in Javascript are not typed so the value of an element can be an object, string, number, boolean, function or an array. The main difference between an array and an object is the length property which has a value greater than the largest integer key in the array.

For example:

You could have an create an empty array and add two elements at index 0 and index 99. The length would be 100, but the number of elements in the array would be 2.

var arr = [];
arr[0] = 0;
arr[99] = {name: "John"};
console.log(arr.length); // prints 100
arr; // prints something like [0, undefined × 98, Object { name: "John"}]

To answer your questions directly:

Q. It is my understanding that I can store mixed data in a JavaScript array, as well as change any element in the array to some other type. How does the interpreter keep track of what place in physical memory any element is at? Also, how is the overwriting of the data in the next element prevented if I change an element to a larger data type?

A. You probably know this by now if you've read my comments above. In Javascript, an array is a Hashtable Object type so the interpreter doesn't need to keep track of physical memory and changing the value of an element doesn't affect other elements as they're not stored in a contiguous block of memory.

--

Q. I assume that arrays only store references to actual objects, and primitives are wrapped behind the scenes when placed in arrays. Assuming this is the case, if I have a different handle on the primitive variable and change the value stored in the array is synchronicity maintained?

A. No, primitives are not wrapped. Changing a primitive that was assigned to an array will not change the value in the array as they're stored by value. Objects on the other hand are stored by reference, so changing the objects value will reflect that change in that array.

Here's an example you can try:

var arr = [];
var obj = { name: "John" };
var isBool = true;

arr.push(obj);
arr[1] = isBool;

console.log(arr[0]); // print obj.name
console.log(arr[1]); // print true

obj.age = 40;        // add age to obj
isBool = false;      // change value for isBool

console.log(arr[0]); // value here will contain age
console.log(arr[1]); // value here will still be true

Also, note that when you initialize an array in the following two ways, it has a different behavior:

var arr = new Array(100);
console.log(arr.length);        // prints 100
console.log(arr);               // prints []

var arr2 = new Array(100, 200);
console.log(arr2.length);       // prints 2
console.log(arr2);              // prints [100, 200]

If you want to use Javascript Arrays as contiguous blocks of memory, you should look into using TypedArray. TypedArray's allow you to allocate a block of memory as a byte array and access the raw binary data more efficiently.

You can learn more about the intricacies of Javascript by reading the ECMA-262 spec (ver 5.1).

这篇关于JavaScript数组在物理内存中的表现如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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