Skip to content

Commit

Permalink
docs: improve docs about TextEncoder to specify encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Feb 28, 2025
1 parent a1bee24 commit 46a2b82
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ If you need at the end a text rather than an Uint8Array it is also extremely fas

```js
const base64 = encode(bytes);
const string = new TextDecoder().decode(base64);
const string = new TextDecoder('utf8').decode(base64);
```

## Installation
Expand Down Expand Up @@ -44,13 +44,13 @@ const bytes = new Uint8Array(256 * 1024 * 1024).map((_, i) =>

console.time('base64');
const base64 = encode(bytes);
const string = new TextDecoder().decode(base64);
const string = new TextDecoder('utf8').decode(base64);
console.timeEnd('base64');

console.log(string.slice(0, 100));
```

This code takes 330ms on my MacBook pro M4 to encode 256Mb of data. The encoding itself takes 230ms while the conversion to text takes 100ms.
This code takes 330ms on my MacBook pro M4 to encode 256Mb of data. The encoding itself takes 240ms while the conversion to text takes 50ms.

### decode

Expand Down
6 changes: 2 additions & 4 deletions benchmark/big.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';

const { Buffer } = require('buffer');
const { Buffer } = require('node:buffer');

const { decode, encode } = require('../lib/');

const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();
const textDecoder = new TextDecoder('utf8');

let string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNIOQRSTUVWXYZ';
for (let i = 0; i < 20; i++) {
Expand Down

0 comments on commit 46a2b82

Please sign in to comment.