# Count UTF8 bytes
----
## WHY
**Requirement:**
max length 5000 in textarea dialog
**Background:**
DB 設定是 varchar(5000) ,也就是可塞 5000 bytes
一個中文字:
* UTF-8 佔 3 bytes
* BIG5 佔 2 bytes
----
## WHAT
[Compare List](https://jsbench.me/ehklab415e/1):
```js
new Blob([input]).size;
```
```js
encodeURI(input).split(/%..|./).length - 1;
```
```js
unescape(encodeURI(input)).length;
```
```js
new TextEncoder().encode(input).length;
```
----
```js
new Blob([input]).size;
```
ref. [MDN](https://developer.mozilla.org/zh-TW/docs/Web/API/Blob/Blob)
* slow
* a newly created Blob object
----
```js
unescape(encodeURI(input)).length;
```
ref. [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape)

----
```js
encodeURI(input).split(/%..|./).length - 1;
```
ref. [MDN](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/encodeURI)
* encodeURI + regular expression
* slow
----
```js
new TextEncoder().encode(input).length;
```
ref. [MDN](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder)
* ```TextEncoder``` takes a stream of code points as input and emits a stream of UTF-8 bytes.
{"metaMigratedAt":"2023-06-15T22:55:33.468Z","metaMigratedFrom":"YAML","title":"Count UTF8 bytes","breaks":true,"description":"View the slide with \"Slide Mode\".","contributors":"[{\"id\":\"3650a878-e127-49c0-8ba0-ac2bbee661bf\",\"add\":1663,\"del\":360}]"}