admin管理员组文章数量:1327933
I think there can be a difference between browers,
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.
Thanks in advance!
I think there can be a difference between browers,
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.
Thanks in advance!
Share Improve this question asked Dec 17, 2012 at 14:16 kaljakkaljak 1,2731 gold badge17 silver badges34 bronze badges 9- 1 Chrome's heap snapshot might help. Hold on. – John Dvorak Commented Dec 17, 2012 at 14:20
- 1 @Jan It does, or at least can: developer.mozilla/en-US/docs/JavaScript/… – deceze ♦ Commented Dec 17, 2012 at 14:20
- @deceze these constructors seem to be firefox-specific. – John Dvorak Commented Dec 17, 2012 at 14:21
- @Jan Sure. The OP could use them at least on FF as browser-specific optimization. – deceze ♦ Commented Dec 17, 2012 at 14:22
- @deceze actually Chrome has these as well. – John Dvorak Commented Dec 17, 2012 at 14:23
1 Answer
Reset to default 6This depends on a wide number of aspects.
- 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).
- If you want a better insight on size/speed/execution times, I think the only accurate way to go is to use the profiling tools that e with the browser, or use addons like FireBug.
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 your COM
and DOM
references once you are done with them.
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 using var
as defining it as a context variable, use myArray.length = 0;
which will delete
the entire storage in the array.
If you are dealing with large collections, direct assignment is faster than using the Array.prototype.push();
method. myArray[i] = 0;
is faster than myArray.push(0);
according to jsPerf. 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;
};
Test cases for the above are available here.
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.
本文标签: javascriptHow much memory does one arrayelement takeStack Overflow
版权声明:本文标题:javascript - How much memory does one array-element take? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742254087a2441306.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论