Answer by Mannan for String length in bytes in JavaScript
str = "Hello, World!";str.length/1024*1024; // here's the number of bytesabove is a shorter way to get the string size in bytes
View ArticleAnswer by ehmicky for String length in bytes in JavaScript
Based on the following benchmarks, this appears to be the fastest choice that works on all platforms:In Node.js: Buffer.byteLength(string).Otherwise:new TextEncoder().encode(string).length on big...
View ArticleAnswer by Zoran for String length in bytes in JavaScript
sizeInBytes = Buffer.from(data).lengthExample:let data = 'šč'; // data with utf-8 charactersconsole.log( data.length ); // 2console.log( Buffer.from(data).length ); // 4
View ArticleAnswer by user13366325 for String length in bytes in JavaScript
I'm checking the length with :const str = "🌧as%20😈"const len = new URL(str.replace(/%[A-F0-9]{2}/g, "..."), "https:$").pathname.replace(/%[A-F0-9]{2}/g, "-").length - 1console.log(len) // 13when i (try...
View ArticleAnswer by mplungjan for String length in bytes in JavaScript
Ran into this How to emulate mb_strlen in javascript with strings containing HTMLwhere the string was not a good match for earlier answers.I got the expected length of 8 here:const str =...
View ArticleAnswer by KevinHJ for String length in bytes in JavaScript
I compared some of the methods suggested here in Firefox for speed.The string I used contained the following characters:😀œ´®†¥¨ˆøπ¬˚∆˙©ƒ∂ßåΩ≈ç√∫˜µ≤All results are averages of 3 runs each. Times are in...
View ArticleAnswer by Boaz for String length in bytes in JavaScript
In NodeJS, Buffer.byteLength is a method specifically for this purpose:let strLengthInBytes = Buffer.byteLength(str); // str is UTF-8Note that by default the method assumes the string is in UTF-8...
View ArticleAnswer by laurent for String length in bytes in JavaScript
Took me a while to find a solution for React Native so I'll put it here:First install the buffer package:npm install --save bufferThen user the node method:const { Buffer } = require('buffer');const...
View ArticleAnswer by Iván Pérez for String length in bytes in JavaScript
Another very simple approach using Buffer (only for NodeJS):Buffer.byteLength(string, 'utf8')Buffer.from(string).length
View ArticleAnswer by simap for String length in bytes in JavaScript
For simple UTF-8 encoding, with slightly better compatibility than TextEncoder, Blob does the trick. Won't work in very old browsers though.new Blob(["😀"]).size; // -> 4
View ArticleAnswer by chrislau for String length in bytes in JavaScript
This would work for BMP and SIP/SMP characters. String.prototype.lengthInUtf8 = function() { var asciiLength = this.match(/[\u0000-\u007f]/g) ? this.match(/[\u0000-\u007f]/g).length : 0; var...
View ArticleAnswer by fuweichin for String length in bytes in JavaScript
Here is an independent and efficient method to count UTF-8 bytes of a string.//count UTF-8 bytes of a stringfunction byteLengthOf(s){//assuming the String is UCS-2(aka UTF-16) encodedvar n=0;for(var...
View ArticleAnswer by Riccardo Galli for String length in bytes in JavaScript
Years passed and nowadays you can do it natively(new TextEncoder().encode('foo')).lengthNote that it's not supported by IE (you may use a polyfill for that).MDN documentationStandard specifications
View ArticleAnswer by lovasoa for String length in bytes in JavaScript
Here is a much faster version, which doesn't use regular expressions, nor encodeURIComponent():function byteLength(str) { // returns the byte length of an utf8 string var s = str.length; for (var...
View ArticleAnswer by anh tran for String length in bytes in JavaScript
You can try this:function getLengthInBytes(str) { var b = str.match(/[^\x00-\xff]/g); return (str.length + (!b ? 0: b.length)); }It works for me.
View ArticleAnswer by Lauri Oherd for String length in bytes in JavaScript
This function will return the byte size of any UTF-8 string you pass to it.function byteCount(s) { return encodeURI(s).split(/%..|./).length - 1;}Source
View ArticleAnswer by Alexander Gladysh for String length in bytes in JavaScript
Actually, I figured out what's wrong. For the code to work the page <head> should have this tag:<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />Or, as suggested in...
View ArticleAnswer by Mike Samuel for String length in bytes in JavaScript
There is no way to do it in JavaScript natively. (See Riccardo Galli's answer for a modern approach.)For historical reference or where TextEncoder APIs are still unavailable.If you know the character...
View ArticleString length in bytes in JavaScript
In my JavaScript code I need to compose a message to server in this format:<size in bytes>CRLF<data>CRLFExample:3fooThe data may contain unicode characters. I need to send them as UTF-8.I'm...
View Article