@atproto/lex-json 0.0.15 β 0.1.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/dist/blob.js +6 -9
- package/dist/blob.js.map +1 -1
- package/dist/bytes.js +5 -9
- package/dist/bytes.js.map +1 -1
- package/dist/index.js +4 -7
- package/dist/index.js.map +1 -1
- package/dist/json-bytes-decoder.d.ts +24 -0
- package/dist/json-bytes-decoder.d.ts.map +1 -0
- package/dist/json-bytes-decoder.js +581 -0
- package/dist/json-bytes-decoder.js.map +1 -0
- package/dist/json.js +1 -2
- package/dist/lex-json.d.ts +7 -3
- package/dist/lex-json.d.ts.map +1 -1
- package/dist/lex-json.js +40 -61
- package/dist/lex-json.js.map +1 -1
- package/dist/link.js +5 -9
- package/dist/link.js.map +1 -1
- package/package.json +6 -7
- package/src/json-bytes-decoder.bench.ts +252 -0
- package/src/json-bytes-decoder.test.ts +889 -0
- package/src/json-bytes-decoder.ts +672 -0
- package/src/lex-json.bench.ts +125 -0
- package/src/lex-json.test.ts +368 -0
- package/src/lex-json.ts +19 -33
- package/src/link.ts +1 -1
- package/tsconfig.build.json +1 -1
- package/tsconfig.tests.json +1 -1
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { bench, describe } from 'vitest'
|
|
2
|
+
import { utf8FromBytes } from '@atproto/lex-data'
|
|
3
|
+
import { JsonBytesDecoder } from './json-bytes-decoder.js'
|
|
4
|
+
import { LexParseOptions, lexParse, lexParseJsonBytes } from './lex-json.js'
|
|
5
|
+
|
|
6
|
+
// This benchmark compares the performance of two implementations of
|
|
7
|
+
// lexParseJsonBytes:
|
|
8
|
+
// 1. lexParseJsonBytesDecoder: An implementation that uses a custom decoder
|
|
9
|
+
// class that operates directly on bytes to parse JSON and handle AT Protocol
|
|
10
|
+
// special types.
|
|
11
|
+
// 2. lexParseJsonBytesNaive: A simpler implementation that first decodes bytes
|
|
12
|
+
// to a UTF-8 string and then uses the existing lexParse function to parse
|
|
13
|
+
// the JSON.
|
|
14
|
+
|
|
15
|
+
describe('small object', () => {
|
|
16
|
+
benchData({
|
|
17
|
+
$type: 'app.bsky.feed.post',
|
|
18
|
+
text: 'Hello world! π',
|
|
19
|
+
createdAt: '2024-01-01T00:00:00Z',
|
|
20
|
+
})
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
describe('simple mixed structure', () => {
|
|
24
|
+
benchData({
|
|
25
|
+
cid: {
|
|
26
|
+
$link: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
27
|
+
},
|
|
28
|
+
bytes: {
|
|
29
|
+
$bytes: 'nFERjvLLiw9qm45JrqH9QTzyC2Lu1Xb4ne6+sBrCzI0',
|
|
30
|
+
},
|
|
31
|
+
blob: {
|
|
32
|
+
$type: 'blob',
|
|
33
|
+
ref: {
|
|
34
|
+
$link: 'bafkreig77vqcdozl2wyk6z3cscaj5q5fggi53aoh64fewkdiri3cdauyn4',
|
|
35
|
+
},
|
|
36
|
+
mimeType: 'image/jpeg',
|
|
37
|
+
size: 10000,
|
|
38
|
+
},
|
|
39
|
+
nested: {
|
|
40
|
+
array: [
|
|
41
|
+
{
|
|
42
|
+
number: 42,
|
|
43
|
+
string: 'hello world',
|
|
44
|
+
bool: true,
|
|
45
|
+
null: null,
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
string: 'Hello δΈη! πππ ΓoΓ±o',
|
|
49
|
+
createdAt: '2024-01-01T00:00:00Z',
|
|
50
|
+
},
|
|
51
|
+
})
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
describe('large structure', () => {
|
|
55
|
+
// Similar to the large object benchmark but smaller
|
|
56
|
+
benchData({
|
|
57
|
+
items: Array.from({ length: 25 }, (_, i) => ({
|
|
58
|
+
id: i,
|
|
59
|
+
name: `Item ${i}`,
|
|
60
|
+
longUnicode:
|
|
61
|
+
'Lorem ipsum dolor sit amet, consectetur adipiscing elit π€©.\n'.repeat(
|
|
62
|
+
3,
|
|
63
|
+
),
|
|
64
|
+
tags: ['tag1', 'tag2', 'tag3'],
|
|
65
|
+
bytes: {
|
|
66
|
+
$bytes: Buffer.from(`This is some byte data for item ${i}`).toString(
|
|
67
|
+
'base64',
|
|
68
|
+
),
|
|
69
|
+
},
|
|
70
|
+
cid: {
|
|
71
|
+
$link: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
72
|
+
},
|
|
73
|
+
metadata: {
|
|
74
|
+
created: '2024-01-01T00:00:00Z',
|
|
75
|
+
count: i * 10,
|
|
76
|
+
nested: {
|
|
77
|
+
flag: i % 2 === 0,
|
|
78
|
+
values: [i, i * 2, i * 3],
|
|
79
|
+
},
|
|
80
|
+
items: Array.from({ length: 5 }, (_, j) => ({
|
|
81
|
+
id: `${i}-${j}`,
|
|
82
|
+
value: `Value ${i}-${j}`,
|
|
83
|
+
})),
|
|
84
|
+
},
|
|
85
|
+
})),
|
|
86
|
+
})
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
describe.skip('extensive test suite', () => {
|
|
90
|
+
describe('integer', () => {
|
|
91
|
+
benchData(42)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
describe('float', () => {
|
|
95
|
+
benchData(42.42, { strict: false })
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
describe('short ascii', () => {
|
|
99
|
+
benchData('hello world')
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
describe('short unicode', () => {
|
|
103
|
+
benchData('Hello δΈη! πππ ΓoΓ±o')
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
describe('Long text with unicode and escaped characters', () => {
|
|
107
|
+
benchData(
|
|
108
|
+
[
|
|
109
|
+
'LorΓ©m ipsum dolor sit amet, consectetur adipiscing elit π€©.',
|
|
110
|
+
'Sed Γ§o eiusmod tempor π§πͺ incididunt ut labore et dolore magna aliqua.',
|
|
111
|
+
'',
|
|
112
|
+
'\tUt enim Β° minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.',
|
|
113
|
+
'\tDuis aute @ dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.',
|
|
114
|
+
'',
|
|
115
|
+
'Excepteur sint Γ΅ cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
|
|
116
|
+
].join('\n'),
|
|
117
|
+
)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
describe('$link', () => {
|
|
121
|
+
benchData({
|
|
122
|
+
$link: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
123
|
+
})
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
describe('$bytes', () => {
|
|
127
|
+
const alphabet =
|
|
128
|
+
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
|
129
|
+
|
|
130
|
+
for (const length of [32, 128, 256, 512, 1024, 1024 * 1024]) {
|
|
131
|
+
describe(String(length), () => {
|
|
132
|
+
benchData({
|
|
133
|
+
$bytes: Array.from(
|
|
134
|
+
{ length },
|
|
135
|
+
(_, i) => alphabet[i % alphabet.length],
|
|
136
|
+
).join(''),
|
|
137
|
+
})
|
|
138
|
+
})
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
describe('small object', () => {
|
|
143
|
+
benchData({
|
|
144
|
+
string: 'hello world',
|
|
145
|
+
number: 42,
|
|
146
|
+
bool: true,
|
|
147
|
+
null: null,
|
|
148
|
+
})
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
describe('medium object', () => {
|
|
152
|
+
benchData({
|
|
153
|
+
user: {
|
|
154
|
+
id: '12345',
|
|
155
|
+
name: 'John Doe',
|
|
156
|
+
email: 'john@example.com',
|
|
157
|
+
active: true,
|
|
158
|
+
score: 95,
|
|
159
|
+
},
|
|
160
|
+
posts: [
|
|
161
|
+
{ id: 1, title: 'First Post', likes: 10 },
|
|
162
|
+
{ id: 2, title: 'Second Post', likes: 25 },
|
|
163
|
+
{ id: 3, title: 'Third Post', likes: 42 },
|
|
164
|
+
],
|
|
165
|
+
metadata: {
|
|
166
|
+
created: '2024-01-01T00:00:00Z',
|
|
167
|
+
updated: '2024-01-15T12:30:00Z',
|
|
168
|
+
},
|
|
169
|
+
})
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
describe('heavy nesting', () => {
|
|
173
|
+
type NestedObject = { level: number; nested?: NestedObject }
|
|
174
|
+
const nestedObject = { level: 0 }
|
|
175
|
+
let current: NestedObject = nestedObject
|
|
176
|
+
for (let i = 1; i <= 100; i++) {
|
|
177
|
+
current.nested = { level: i }
|
|
178
|
+
current = current.nested
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
benchData(nestedObject)
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
describe('number heavy array', () => {
|
|
185
|
+
benchData([
|
|
186
|
+
0,
|
|
187
|
+
1,
|
|
188
|
+
-1,
|
|
189
|
+
42,
|
|
190
|
+
-100,
|
|
191
|
+
9007199254740991,
|
|
192
|
+
42,
|
|
193
|
+
-1000,
|
|
194
|
+
9007199254740991,
|
|
195
|
+
Date.now(),
|
|
196
|
+
...Array.from({ length: 100 }, (_, i) =>
|
|
197
|
+
Math.floor(i * 1000 * Math.random()),
|
|
198
|
+
),
|
|
199
|
+
])
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
describe('many empty objects', () => {
|
|
203
|
+
benchData(Array.from({ length: 200 }, () => ({})))
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
describe('many small objects', () => {
|
|
207
|
+
benchData(
|
|
208
|
+
Array.from({ length: 200 }, (_, i) => ({
|
|
209
|
+
id: i,
|
|
210
|
+
name: `item${i}`,
|
|
211
|
+
active: true,
|
|
212
|
+
})),
|
|
213
|
+
)
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
describe('many empty arrays', () => {
|
|
217
|
+
benchData(Array.from({ length: 200 }, () => []))
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
describe('many small arrays', () => {
|
|
221
|
+
benchData(Array.from({ length: 200 }, (_, i) => [i, '', true]))
|
|
222
|
+
})
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
function benchData(data: unknown, options?: LexParseOptions) {
|
|
226
|
+
const bytes = Buffer.from(JSON.stringify(data))
|
|
227
|
+
|
|
228
|
+
const lexParseJsonBytesDecoder: typeof lexParseJsonBytes = (
|
|
229
|
+
bytes,
|
|
230
|
+
options,
|
|
231
|
+
) => {
|
|
232
|
+
const decoder = new JsonBytesDecoder(bytes, options?.strict)
|
|
233
|
+
return decoder.decode()
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const lexParseJsonBytesNaive: typeof lexParseJsonBytes = (bytes, options) => {
|
|
237
|
+
const jsonString = utf8FromBytes(bytes)
|
|
238
|
+
return lexParse(jsonString, options)
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
bench('current', () => {
|
|
242
|
+
lexParseJsonBytes(bytes, options)
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
bench(lexParseJsonBytesDecoder, () => {
|
|
246
|
+
lexParseJsonBytesDecoder(bytes, options)
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
bench(lexParseJsonBytesNaive, () => {
|
|
250
|
+
lexParseJsonBytesNaive(bytes, options)
|
|
251
|
+
})
|
|
252
|
+
}
|