javascript中不同数据类型的内存使用情况 [英] Memory Usage Of Different Data Types in javascript
问题描述
我遇到的一个问题是,javascript中的不同数据类型有多少使用内存。例如,在C ++数据类型中,如int,char,float使用2,1,8字节的内存顺序。现在数据类型像数字,字符串,布尔值,空,undefind和对象,javascript中的数组使用多少内存以及接受的范围是多少?
因为我的英语水平低而接受我的道歉!!!
A question that has happened to me is that different Data type in javascript how many use of memory . for Example in C++ data type like int , char , float uses order 2 , 1 , 8 byte of memory . now data Type like Number , string , boolean , null , undefind and Objects , Arrays in javascript how many use of memory and what is ranges that accepted ? Accept my apologize because of my low English level!!!
推荐答案
数字是8个字节。
在 w3schools页面中找到。
我为其他JavaScript基元类型搜索了一下,但是很难找到这些信息!我确实找到了以下代码:
I searched around a bit more for other JavaScript primitive types, but it's surprisingly hard to find this information! I did find the following code though:
...
if ( typeof value === 'boolean' ) {
bytes += 4;
}
else if ( typeof value === 'string' ) {
bytes += value.length * 2;
}
else if ( typeof value === 'number' ) {
bytes += 8;
}
...
似乎表示字符串是2个字节每个字符,布尔值是4个字节。
Seems to indicate that a String is 2 bytes per character, and a boolean is 4 bytes.
Found that code here and here. The full code's actually used to get the rough size of an object.
虽然在进一步阅读后,我发现了这个有趣的代码konijn :计算字符串的字节长度。
Although, upon further reading, I found this interesting code by konijn on this page: Count byte length of string.
function getByteCount( s )
{
var count = 0, stringLength = s.length, i;
s = String( s || "" );
for( i = 0 ; i < stringLength ; i++ )
{
var partCount = encodeURI( s[i] ).split("%").length;
count += partCount==1?1:partCount-1;
}
return count;
}
getByteCount("i♥js"); // 6 bytes
getByteCount("abcd"); // 4 bytes
因此,内存中字符串的大小似乎取决于字符本身。虽然我仍在试图找出为什么他将数量设置为1,如果它是1,否则他拿了 count-1
(在for循环中)。
So it seems that the string's size in memory depends on the characters themselves. Although I am still trying to figure out why he set the count to 1 if it's 1, otherwise he took count-1
(in the for loop).
如果我发现其他任何内容,我会更新帖子。
Will update post if I find anything else.
这篇关于javascript中不同数据类型的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!