javascript javascript中不同数据类型的内存使用情况

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4905861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:19:00  来源:igfitidea点击:

Memory Usage Of Different Data Types in javascript

javascripttypes

提问by A.B.Developer

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!!!

我遇到的一个问题是 javascript 中的不同数据类型有多少内存使用。例如在 C++ 数据类型中,如 int 、 char 、 float 使用顺序 2 、 1 、 8 字节的内存。现在数据类型如数字、字符串、布尔值、空值、未定义和对象,javascript 中的数组有多少内存使用以及接受的范围是多少?接受我的道歉,因为我的英语水平低!!!

采纳答案by Rani Kheir

Numbers are 8 bytes.

数字是 8 个字节。

Found that in this w3schools page.

在这个w3schools 页面中发现

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:

我搜索了更多其他 JavaScript 原始类型,但很难找到这些信息!我确实找到了以下代码:

    ...
    if ( typeof value === 'boolean' ) {
        bytes += 4;
    }
    else if ( typeof value === 'string' ) {
        bytes += value.length * 2;
    }
    else if ( typeof value === 'number' ) {
        bytes += 8;
    }
    ...

Seems to indicate that a String is 2 bytes per character, and a boolean is 4 bytes.

似乎表明一个字符串是每个字符 2 个字节,一个布尔值是 4 个字节。

Found that code hereand here. The full code's actually used to get the rough size of an object.

在此处此处找到该代码。完整的代码实际上用于获取对象的粗略大小。

Although, upon further reading, I found this interesting code by konijnon this page: Count byte length of string.

虽然,在进一步阅读后,我在此页面上发现了konijn 的这个有趣的代码: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

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).

所以似乎字符串在内存中的大小取决于字符本身。尽管我仍在试图弄清楚为什么如果计数为 1,他将计数设置为 1,否则他count-1(在 for 循环中)接受了。

Will update post if I find anything else.

如果我发现其他任何东西,将更新帖子。

回答by superjos

As of today, MDN Data Structurespage gives some more info about it:

截至今天,MDN 数据结构页面提供了有关它的更多信息:

Number

数字

According to the ECMAScript standard, there is only one number type: the double-precision 64-bit binary format IEEE 754 value

根据 ECMAScript 标准,只有一种数字类型:双精度 64 位二进制格式 IEEE 754 值

So that should amount to 8 bytes.

所以这应该是 8 个字节。

String

细绳

JavaScript's String type is used to represent textual data. It is a set of "elements" of 16-bit unsigned integer values.

JavaScript 的 String 类型用于表示文本数据。它是一组 16 位无符号整数值的“元素”。

So that should amount to 2 bytes per character.

所以这应该相当于每个字符 2 个字节。

Boolean

布尔值

Boolean represents a logical entity and can have two values: true, and false.

Boolean 代表一个逻辑实体,可以有两个值:true 和 false。

Nothing more about that.

仅此而已。