{

How to get string size in bytes in javascript examples


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

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

  • using a 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 one character returns 1 byte in characters, whereas “ab” returns 2 bytes. However, emojis returns 4 bytes for each symbol, two emojis returns 8 bytes.

Emojis are UTF encoding strings that use 4 bytes to represent a character.

And the output of the above input is

1
2
4
4
6

using a nodejs buffer object

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

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

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

Here is a 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 a javascript inbuilt object used to get binary data of any type of file. The file can be images, string text.

Blob.size returns the size of a 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 web workers 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. It uses in encoding the string.

However, the length method of the unescape object can return the string 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 the appropriate one 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.