


Javascript calculates the number of bytes occupied by a string in localStorage_javascript tips
A recent project has a requirement to use js to calculate the memory occupied by a string written to localStorage. As we all know, js uses Unicode encoding. There are N types of Unicode implementations, among which UTF-8 and UTF-16 are the most commonly used. Therefore, this article only discusses these two encodings.
The following definition is taken from Wikipedia (http://zh.wikipedia.org/zh-cn/UTF-8), with some deletions.
UTF-8 (8-bit Unicode Transformation Format) is a variable-length character encoding for Unicode that can represent any character in the Unicode standard, and the first byte in its encoding is still compatible with ASCII , using one to four bytes to encode each character
The encoding rules are as follows:
Character codes between 000000 – 00007F are encoded with one byte;
Characters between 000080 – 0007FF use two bytes;
Use three bytes between 000800 – 00D7FF and 00E000 – 00FFFF. Note: Unicode does not have any characters in the range D800-DFFF;
Use 4 bytes between 010000 – 10FFFF.
UTF-16 is a fixed-length character encoding. Most characters use two bytes to encode, and character codes exceeding 65535 use four bytes, as follows:
000000 – 00FFFF two bytes;
010000 – 10FFFF four bytes.
At first I thought that since the page is encoded in UTF-8, the strings stored in localStorage should also be encoded in UTF-8. But later tests found that the calculated size was less than 5MB, but an exception was thrown when saving it to localStorage. After thinking about it, the encoding of the page can be changed. If localStorage stores strings according to the encoding of the page, wouldn't it be a mess? Browsers should all use UTF-16 encoding. The 5MB string was calculated using UTF-16 encoding, and it was successfully written. If it exceeds, it fails.
Okay, here’s the code implementation. The calculation rules are as written above. For calculation speed, the two for loops are written separately.
/** * 计算字符串所占的内存字节数,默认使用UTF-8的编码方式计算,也可制定为UTF-16 * UTF-8 是一种可变长度的 Unicode 编码格式,使用一至四个字节为每个字符编码 * * 000000 - 00007F(128个代码) 0zzzzzzz(00-7F) 一个字节 * 000080 - 0007FF(1920个代码) 110yyyyy(C0-DF) 10zzzzzz(80-BF) 两个字节 * 000800 - 00D7FF 00E000 - 00FFFF(61440个代码) 1110xxxx(E0-EF) 10yyyyyy 10zzzzzz 三个字节 * 010000 - 10FFFF(1048576个代码) 11110www(F0-F7) 10xxxxxx 10yyyyyy 10zzzzzz 四个字节 * * 注: Unicode在范围 D800-DFFF 中不存在任何字符 * {@link http://zh.wikipedia.org/wiki/UTF-8} * * UTF-16 大部分使用两个字节编码,编码超出 65535 的使用四个字节 * 000000 - 00FFFF 两个字节 * 010000 - 10FFFF 四个字节 * * {@link http://zh.wikipedia.org/wiki/UTF-16} * @param {String} str * @param {String} charset utf-8, utf-16 * @return {Number} */ var sizeof = function(str, charset){ var total = 0, charCode, i, 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; }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...
