How to get string size in bytes in javascript examples


String in JavaScript is a group of characters, Each characters holds 16 bits of integer. Each string store the data in memory , as we don’t have direct method like sizeof in c programming language

However, We can find string size in bytes in multiple ways

  • using nodejs buffer object
  • Using Blob object
  • TextEncoder encoder method
  • unescape method

Let’s declare the input string

var str="a";
var str1="ab";
var str2="aabc";
var str3="🙂";
var str4="👍🙂";

input a one character which returns 1 byte in characters, where as “ab” returns 2 bytes However emojis returns 4 byes for each symbol, two emojis returns 8 bytes

Emojis are UTF encoding strings which uses 4 bytes to represent an characters

And the output of above input is

1
2
4
4
6

using nodejs buffer object

Buffer is an inbuilt object in NodeJS programming used to represents the fixed bytes.

It supports latest browsers and widely used in JavaScript framework. It works in server side

Buffer’s byteLength method return string size in bytes with encoding

Here is an syntax

Buffer.byteLength(String, encoding)

String - string raw data encoding - UTF-8 or UTF-16

It returns String size in bytes


console.log(Buffer.byteLength(str, 'utf8'));
console.log(Buffer.byteLength(str1, 'utf8'));
console.log(Buffer.byteLength(str2, 'utf8'));
console.log(Buffer.byteLength(str3, 'utf8'));
console.log(Buffer.byteLength(str4, 'utf8'));

Blob object to calculate string size in bytes

Blob is an javascript inbuilt object used to get binary data of an any type of file. The file can be images,string text.

Blob.size returns the size of an string



console.log(new Blob([str]).size);
console.log(new Blob([str1]).size);
console.log(new Blob([str2]).size);
console.log(new Blob([str3]).size);
console.log(new Blob([str4]).size);

TextEncoder encode function

TextEncoder is a javascript used in webworkers to return the string in bytes encode method returns the encoded object and length returns in bytes

console.log(new TextEncoder().encode(str).length);
console.log(new TextEncoder().encode(str1).length);
console.log(new TextEncoder().encode(str2).length);
console.log(new TextEncoder().encode(str3).length);
console.log(new TextEncoder().encode(str4).length);

unescape length method

unescape is an object going to be deprecated This will be used in encoding the string.

However, length method of unescape object can returns the stirng data in bytes



console.log(unescape(encodeURIComponent(str)).length);
console.log(unescape(encodeURIComponent(str1)).length);
console.log(unescape(encodeURIComponent(str2)).length);
console.log(unescape(encodeURIComponent(str3)).length);
console.log(unescape(encodeURIComponent(str4)).length);

Conclusion

To Sum Up, There are several methods documented in this post to return the string size in bytes. You can choose appropriate on based on your needs.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.