How to get string size in bytes in javascript examples

Different ways to find string size in bytes.

  • nodejs buffer object, an example is Buffer.byteLength("str", 'utf8'))
  • Using Blob object, new Blob([str]).size
  • TextEncoder encoder method, new TextEncoder().encode(str).length
  • unescape method,unescape(encodeURIComponent(str)).length

A 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.

You can check my other post on Javascript check substring exists in a String 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 return 4 bytes for each symbol and two emojis return 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;

Find String size 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 in javascript

BlobπŸ”— is a javascript inbuilt object used to get binary data of any type of file. The file can be images or 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);

Find String Length TextEncoder encode function

TextEncoderπŸ”— is a javascript used by web workers to return the string in bytes The encode method returns the encoded object and the 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);

Find String Length using 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.