-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
80 lines (63 loc) · 2.03 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import testData from './testData.json'
import { copyString, compress, compressString, decompressString } from './lz4'
import { baseline, bench, clear, group, run } from "tatami-ng"
import { UsersSchema } from './testDataZodSchema'
import Pbf from 'pbf'
import { writeUsers, readUsers } from './testDataGeneratedProtoInterface.mjs'
const jsonAsTypedArray = new TextEncoder().encode(JSON.stringify(testData))
const compressed = compress(jsonAsTypedArray)!
console.log(`compressed: ${compressed.size} decompressed: ${jsonAsTypedArray.byteLength} ratio: ${compressed.size / jsonAsTypedArray.byteLength}`)
group('round-trip', () => {
baseline('json', () => {
JSON.parse(JSON.stringify(testData))
})
bench('json with zod', () => {
UsersSchema.parse(JSON.parse(JSON.stringify(testData)))
})
bench('protocol buffer', () => {
const writePbf = new Pbf()
writeUsers({ users: testData }, writePbf)
const protocolBuffer = writePbf.finish()
readUsers(new Pbf(protocolBuffer))
})
bench('json with lz4', () => {
JSON.parse(decompressString(compressString(JSON.stringify(testData))!)!)
})
bench('string copy like lz4', () => {
copyString(JSON.stringify(testData))
})
})
group('write', () => {
baseline('json', () => {
JSON.stringify(testData)
})
bench('protocol buffer', () => {
const writePbf = new Pbf()
writeUsers({ users: testData }, writePbf)
writePbf.finish()
})
bench('json with lz4', () => {
compressString(JSON.stringify(testData))
})
})
const json = JSON.stringify(testData)
const writePbf = new Pbf()
writeUsers({ users: testData }, writePbf)
const protocolBuffer = writePbf.finish()
const compressedJson = compressString(json)!
group('read', () => {
baseline('json', () => {
JSON.parse(json)
})
bench('json with zod', () => {
UsersSchema.parse(JSON.parse(json))
})
bench('protocol buffer', () => {
readUsers(new Pbf(protocolBuffer))
})
bench('json with lz4', () => {
JSON.parse(decompressString(compressedJson)!)
})
})
await run({ colors: true })
clear()