Calculate the Number of Bytes Occupied By a String

Calculate the Number of Bytes Occupied By a String

January 3, 2020
Frontend, Web
★ Star me on GitHub

Calculates the number of memory bytes occupied by the string, which is calculated by default using UTF-8 encoding or can be set to UTF-16.

UTF-8 is a variable-length Unicode encoding format that uses one to four bytes to encode each character

  • 000000 - 00007F (128 codes) 0zzzzzzz (00-7F): one byte
  • 000080 - 0007FF (1920 codes) 110yyyyy(C0-DF) 10zzzzzz(80-BF)`: two bytes
  • 000800 - 00D7FF, 00E000 - 00FFFF(61440 codes) 1110xxxx(E0-EF) 10yyyyyyy 10zzzzzz: three bytes
  • 010000 - 10FFFF(1048576 codes) 11110www(F0-F7) 10xxxxxx 10yyyyy 10zzzzzz:

  • Note: Unicode does not have any characters in the range D800-DFFF
  • UTF-16 mostly uses two-byte encoding and four-byte encoding beyond 65535
  • 000000 - 00FFFF two bytes.
  • 010000 - 10FFFF four bytes
// {@link http://en.wikipedia.org/wiki/UTF-16}
// @param  {String} str 
// @param  {String} charset utf-8, utf-16
// @return {Number}
export const stringSize = function (str, charset) {
  let total = 0
  let charCode
  let i
  let len
  charset = charset ? charset.toLowerCase() : ''
  if (charset === 'utf-16' || charset === 'utf16') {
    for (i = 0, len = str.length; i < len; i++) {
      charCode = str.charCodeAt(i)
      if (charCode <= 0xffff) {
        total += 2
      } else {
        total += 4
      }
    }
  } else {
    for (i = 0, len = str.length; i < len; i++) {
      charCode = str.charCodeAt(i)
      if (charCode <= 0x007f) {
        total += 1
      } else if (charCode <= 0x07ff) {
        total += 2
      } else if (charCode <= 0xffff) {
        total += 3
      } else {
        total += 4
      }
    }
  }
  return total
}