Memory Usage Of Different Data Types in javascript -
a question has happened me different data type in javascript how many use of memory . example in c++ data type int , char , float uses order 2 , 1 , 8 byte of memory . data type number , string , boolean , null , undefind , objects , arrays in javascript how many use of memory , ranges accepted ? accept apologize because of low english level!!!
numbers 8 bytes.
found in w3schools page.
i searched around bit more other javascript primitive types, it's surprisingly hard find information! did find 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 indicate string 2 bytes per character, , boolean 4 bytes.
found code here , here. full code's used rough size of object.
although, upon further reading, found interesting code konijn on page: count byte length of string.
function getbytecount( s ) { var count = 0, stringlength = s.length, i; s = string( s || "" ); for( = 0 ; < 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 seems string's size in memory depends on characters themselves. although still trying figure out why set count 1 if it's 1, otherwise took count-1
(in loop).
will update post if find else.
Comments
Post a Comment