Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory Usage Of Different Data Types in 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!!!

like image 657
A.B.Developer Avatar asked Feb 05 '11 08:02

A.B.Developer


People also ask

How much memory does string take in JavaScript?

The string in JavaScript is treated as a sequence of UTF-16 code units and some character takes up 16 bits memory space and others 32 bits.

Which data type can hold more information in JavaScript?

Arrays. An array can hold multiple values within a single variable. This means that you can contain a list of values within an array and iterate through them.

How much memory does a JavaScript object use?

JavaScript uses double-precision (64-bit) floating point numbers. 64 bits are 8 bytes, but each number actually takes up an average of 9.7 bytes. Likewise, Chrome shows the size of each individual empty array as 32 bytes and the size of each empty object as 56 bytes.


2 Answers

Numbers are 8 bytes.

Found that in this w3schools page.

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;     }     ... 

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.

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 

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.

like image 61
Rani Kheir Avatar answered Oct 09 '22 03:10

Rani Kheir


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

Number

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

So that should amount to 8 bytes.

String

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

So that should amount to 2 bytes per character.

Boolean

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

Nothing more about that.

like image 32
superjos Avatar answered Oct 09 '22 04:10

superjos