Memory usage of different data types in javascript

The question that happened to me is that different types of data in javascript use a lot of memory. for example, in C ++, a data type of type int, char, float uses the order of 2, 1, 8 bytes of memory. now data Type type Number, string, boolean, null, undefind and Objects, Arrays in javascript, how much memory is used and what ranges that were accepted? Please accept my apologies for my low level of English !!!

+7
source share
2 answers

Numbers are 8 bytes.

Found on this page w3schools .

I searched a bit more for other primitive JavaScript types, but it is surprisingly hard to find this information! However, I found the following code:

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

It seems to indicate that the string has 2 bytes per character, and the logical one has 4 bytes.

Code found here and here . The complete code actually used to get the approximate size of the object.

Although after further reading, I found this interesting konijn code on this page: The number of bytes in bytes is a 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 

Thus, the line size in memory depends on the characters themselves. Although I'm still trying to understand why he set the counter to 1, if it is 1, otherwise he took count-1 (in the for loop).

The post will be updated if I find anything else.

+9
source

To date, the MDN Data Structures page provides additional information about this:

room

In accordance with the ECMAScript standard, there is only one type of number: the IEEE 754 64-bit binary double-precision format

So this should be 8 bytes.

Line

JavaScript string type is used to represent text data. This is the set of "elements" of unsigned 16-bit integer values.

So this should be 2 bytes per character.

Boolean

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

Nothing more about that.

+4
source

All Articles