一个数组元素需要多少内存?
我认为浏览器之间可能存在差异,
I think there can be a difference between browers,
但是如何在Javascript中找出数组或其中一个元素需要多少内存?
but how do I find out how much memory an array or one element of it takes in Javascript?
我想知道在使用类型数组时我节省了多少空间。
I want to figure out how much space I save when using a typed array.
提前致谢!
这取决于很多方面。
- 用于存储变量的引用的长度可以在大小上变化(如果您没有使用JS中实际不存在的关联数组,但这是一个不同的讨论)。
- 项目本身的大小也各不相同。基本上,用于存储特定类型对象的二进制表示形式是存储器的内容。
- 8位int使用1个字节。
- 16位int使用2个字节。
- 字符串中的字符使用2或4个字节(因为UTF-16)。
- The length of the reference used to store the variable can vary in size (If you are not using associative arrays, which don't actually exist in JS, but that's a different discussion).
- The item itself can also vary in size. Basically, the binary representation used to store the certain type of object is what makes the memory.
- An 8 bit int uses 1 byte.
- A 16 bit int uses 2 bytes.
- A character in a string uses either 2 or 4 bytes (because of UTF-16).
现实情况不同。您不应该担心客户端存储。至少不是这样的。您真正需要的唯一内存特定控件是 memoization 。除此之外,您需要做的就是清理您的痕迹,可以这么说。如果您处于OOP环境中,请确保始终
删除
实例引用并清空COM
和 DOM 引用。The reality is different. You shouldn't worry much about client side storage. Not in this way at least. The only memory specific control you really need is memoization. Other than that, all you need to do is to clean up your traces, so to speak. If you are in an OOP environment, make sure to always
delete
instance references and null yourCOM
andDOM
references once you are done with them.如果要清空集合(数组),只需使用
delete
运算符。如果您没有将集合定义为实例属性,而是使用var
将其定义为上下文变量,请使用myArray.length = 0;
这将删除
数组中的整个存储。If you wish to empty a collection (array), simply use the
delete
operator. If you are not defining the collection as an instance property, but instead you are usingvar
as defining it as a context variable, usemyArray.length = 0;
which willdelete
the entire storage in the array.如果您正在处理大型集合,直接分配比使用 Array.prototype.push(); 方法。
myArray [i] = 0;
比myArray.push(0);
快于jsPerf.com 测试用例。If you are dealing with large collections, direct assignment is faster than using the
Array.prototype.push();
method.myArray[i] = 0;
is faster thanmyArray.push(0);
according to jsPerf.com test cases.根据您引用数组长度的方式以及您使用的互联网浏览器,迭代数组所花费的时间可能会发生很大变化。
The amount of time it takes to iterate over an array can vastly change depending on how you reference the array length, as well as the internet browser you use.
// Option 1 for (var i = 0; i < someArray.length; i++) { // do something; }; // Option 2 var l = myArray.length; for(var i = 0; i < l; i++) { // do something }; // Option 3 for (var i = 0, ii = someArray.length; i < ii; i++) { // do something; };
上述测试用例可用这里。
截至2015年6月6日,相对速度如下:
As of June 6, 2015, the relative speeds are as follows:
+--------+------------+---------------+----------------------+ | Option | Firefox | Google Chrome | Internet Explorer 11 | +--------+------------+---------------+----------------------+ | 1 | Fastest | 0.35% Slower | 46% Slower | | 2 | 21% Slower | 0.15% Slower | 0.09% Slower | | 3 | 21% Slower | Fastest | Fastest | +--------+------------+---------------+----------------------+
在定义数组时,绝对没有必要指定长度,特别是如果你正在进行延迟初始化。
When defining arrays, there is absolutely no point to specify the length, especially if you are doing deferred initialization.
// Doing this is pointless in JS. It will result in an array with 100 undefined values. var a = new Array(100); // Not even this is the best way. var a = new Array(); // Using the array literal is the fastest and easiest way to do things. var a = [];
数组定义的测试用例可用这里。
Test cases for array definition are available here.