How to get string size in bytes in javascript(examples)
- Admin
- Mar 10, 2024
- Typescript Javascript
Different ways to find string size or length in bytes In Javascript
- nodejs
buffer
object, an example isBuffer.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.
The output of the above input is
1;
2;
4;
4;
6;
Use nodejs buffer object
Bufferπ is an inbuilt object in NodeJS programming, used to store the fixed bytes.
Bufferβs byteLength
method returns string size in bytes with a given 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")); // 1
console.log(Buffer.byteLength(str1, "utf8")); // 2
console.log(Buffer.byteLength(str2, "utf8")); // 4
console.log(Buffer.byteLength(str3, "utf8")); // 4
console.log(Buffer.byteLength(str4, "utf8")); // 8
This approach works in the latest browsers and is widely used on the Client and server side.
Use Blob object
Blobπ is a javascript inbuilt object, used to get binary data of any type of file.
The file can be images or string text. Create a Blob object using String, called Blob.size
property returns the size of a string.
console.log(new Blob([str]).size); // 1
console.log(new Blob([str1]).size); // 2
console.log(new Blob([str2]).size); // 4
console.log(new Blob([str3]).size); // 4
console.log(new Blob([str4]).size); // 8
Use TextEncoder encode function
TextEncoderπ is a function used by web workers to return the string data 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);
Use the unescape length method
unescapeπ is an object going to be deprecated. It is used 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.