Skip to content

Commit 788f81f

Browse files
committed
update readme
1 parent 370100f commit 788f81f

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ This library is publised as [@msgpack/msgpack](https://www.npmjs.com/package/@ms
3939
npm install @msgpack/msgpack
4040
```
4141

42+
## Encode and Decode
43+
44+
### `encode(data: unknown, options?): Uint8Array`
45+
46+
It encodes `data` and returns a byte array as `Uint8Array`.
47+
48+
### `decode(buffer: ArrayLike<number> | Uint8Array, options?): unknown`
49+
50+
It decodes `buffer` in a byte buffer and returns decoded data as `uknown`.
51+
52+
### `decodeAsync(stream: AsyncIterable<ArrayLike<number> | Uint8Array>, options?): Promise<unknown>`
53+
54+
It decodes `stream` in an async iterable of byte arrays and returns decoded data as `uknown` wrapped in `Promise`. This function works asyncronously.
55+
4256
## Extension Types
4357

4458
To handle [MessagePack Extension Types](https://github.com/msgpack/msgpack/blob/master/spec.md#extension-types), this library provides `ExtensionCodec` class.

benchmark/sync-vs-async.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!ts-node
2+
/* eslint-disable no-console */
3+
4+
import { encode, decode, decodeAsync } from "../src";
5+
import { writeFileSync, unlinkSync, readFileSync, createReadStream } from "fs";
6+
import { deepStrictEqual } from "assert";
7+
8+
(async () => {
9+
const data = [];
10+
for (let i = 0; i < 1000; i++) {
11+
const id = i + 1;
12+
data.push({
13+
id,
14+
score: Math.round(Math.random() * Number.MAX_SAFE_INTEGER),
15+
title: `Hello, world! #${id}`,
16+
content: `blah blah blah `.repeat(20).trim(),
17+
createdAt: new Date(),
18+
});
19+
}
20+
const encoded = encode(data);
21+
const file = "benchmark/tmp.msgpack";
22+
writeFileSync(file, encoded);
23+
process.on("exit", () => unlinkSync(file));
24+
console.log(`encoded size ${Math.round(encoded.byteLength / 1024)}KiB`);
25+
26+
deepStrictEqual(decode(readFileSync(file)), data);
27+
deepStrictEqual(await decodeAsync(createReadStream(file)), data);
28+
29+
// sync
30+
console.time("readFileSync |> decode");
31+
for (let i = 0; i < 100; i++) {
32+
decode(readFileSync(file));
33+
}
34+
console.timeEnd("readFileSync |> decode");
35+
36+
// async
37+
console.time("creteReadStream |> decodeAsync");
38+
for (let i = 0; i < 100; i++) {
39+
await decodeAsync(createReadStream(file));
40+
}
41+
console.timeEnd("creteReadStream |> decodeAsync");
42+
})();

0 commit comments

Comments
 (0)