Get Byte size of the string in Javascript
Rajnish Katharotiya
Posted on April 12, 2020
Hello folks, Welcome again in a new episode of series called javascript useful snippets. In this series, I’m going to talk about some shortcodes and useful functions of javascript. These snippets can help you to make your development more efficient and faster. Stay tuned till the end to learn something new… 😊
Javascript Useful Snippets — byteSize()
As we all know a byte is one the unit of digital information, and while development taking care of the size of variables, records and files are a much important task. To do that we have various ways but with these functions, it’s very easy to do. byteSize() snippet will take a string as an input and in the output, it’ll return byte size of a given string. Let’s look at the syntax…
const byteSize = str => new Blob([str]).size;
Here, in return, we are using Blob web API to get out byte size. Where Blobs allow you to construct file-like objects and here we are passing our string in array to create one, from that we are returning just size which will be byte size. Let’s see some results in a better understanding…
Result One:
const result = byteSize(“Hello World”) // output: 11
Result Two:
const result = byteSize(“😃”) // output: 4
As we see both results, with plain strings it’s returning the same number as a length while in case of emoji it’s 4 sizes of bytes. ( For knowledge — The size of the UTF encoding simply defines the minimum number of bytes to be used to represent a character. However, certain characters, like the emoji you are using, require more than 2 bytes to be represented.)
Thank you for watching/reading folks, if you found this informative and wanted to make me more content like this please support me on Patreon.
Now, Guys in the next episode I’m going to share a function to get a difference of two arrays. so follow/subscribe to get notification…
Subscribe on youtube https://www.youtube.com/channel/UCvNjso_gPQIPacA6EraoZmg
Facebook: https://www.facebook.com/KatharotiyaRajnish/
Twitter: https://twitter.com/tutorial_spot
Posted on April 12, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.