Quantcast
Channel: String length in bytes in JavaScript - Stack Overflow
Viewing all articles
Browse latest Browse all 19

Answer by lovasoa for String length in bytes in JavaScript

$
0
0

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 i=str.length-1; i>=0; i--) {    var code = str.charCodeAt(i);    if (code > 0x7f && code <= 0x7ff) s++;    else if (code > 0x7ff && code <= 0xffff) s+=2;    if (code >= 0xDC00 && code <= 0xDFFF) i--; //trail surrogate  }  return s;}

Here is a performance comparison.

It just computes the length in UTF8 of each unicode codepoints returned by charCodeAt() (based on wikipedia's descriptions of UTF8, and UTF16 surrogate characters).

It follows RFC3629 (where UTF-8 characters are at most 4-bytes long).


Viewing all articles
Browse latest Browse all 19

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>