Skip to content

Commit 1ca0ef1

Browse files
committed
🎉 feat: hook compression
1 parent 64d8739 commit 1ca0ef1

File tree

9 files changed

+71
-52
lines changed

9 files changed

+71
-52
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 1.2.11
22
Feature:
33
- Compressed lifecycle event
4+
- Lazily build radix tree for dynamic router
45

56
# 1.2.10 - 5 Jan 2025
67
Feature:

bun.lockb

0 Bytes
Binary file not shown.

example/a.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Elysia, t } from '../src'
22
import { req } from '../test/utils'
33

4-
const app = new Elysia().get('/', 'Static Content')
4+
const app = new Elysia().get('/', () => 'Static Content')
55

6-
console.log(app.router.static.http.static['/'])
6+
console.dir(app, { depth: 10 })

example/stress/instance.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
import { Elysia, t } from '../../src'
22

3-
const total = 10_000
3+
const total = 100
44

5-
const setup = () => new Elysia()
5+
const setup = (n = 0) => {
6+
const app = new Elysia()
67

8+
for(let i = 0; i < 10; i++) {
9+
app.get(`/:a/${i + (n * total)}`, () => i)
10+
}
11+
12+
return app
13+
}
14+
15+
const app = new Elysia()
16+
17+
const memory = process.memoryUsage().heapTotal / 1024 / 1024
718
const t1 = performance.now()
8-
for (let i = 0; i < total; i++) setup()
919

20+
for (let i = 0; i < total; i++) app.use(setup(i))
21+
22+
const memoryAfter = process.memoryUsage().heapTotal / 1024 / 1024
1023
const took = performance.now() - t1
1124

1225
console.log(
1326
Intl.NumberFormat().format(total),
14-
'instances took',
27+
'routes took',
1528
+took.toFixed(4),
1629
'ms'
1730
)
18-
console.log('Average', +(took / total).toFixed(4), 'ms / instance')
31+
console.log('Average', +(took / total).toFixed(4), 'ms / route')
32+
console.log(memoryAfter - memory, 'MB memory used')

example/stress/multiple-routes.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
import { Elysia } from '../../src'
22

3-
const app = new Elysia({ precompile: true })
3+
const total = 1000
44

5-
const total = 200
6-
const t = performance.now()
5+
{
6+
console.log('Elysia')
77

8-
const memory = process.memoryUsage().heapTotal / 1024 / 1024
8+
const app = new Elysia({ precompile: true })
9+
const t = performance.now()
10+
const memory = process.memoryUsage().heapTotal / 1024 / 1024
911

10-
for (let i = 0; i < total; i++) app.get(`/id/${i}`, () => 'hello')
12+
for (let i = 0; i < total; i++) app.get(`/id/${i}`, () => 'hello')
1113

12-
const memoryAfter = process.memoryUsage().heapTotal / 1024 / 1024
14+
const memoryAfter = process.memoryUsage().heapTotal / 1024 / 1024
15+
const took = performance.now() - t
1316

14-
const took = performance.now() - t
17+
console.log(
18+
Intl.NumberFormat().format(total),
19+
'routes took',
20+
+took.toFixed(4),
21+
'ms'
22+
)
23+
console.log('Average', +(took / total).toFixed(4), 'ms / route')
1524

16-
console.log(
17-
Intl.NumberFormat().format(total),
18-
'routes took',
19-
+took.toFixed(4),
20-
'ms'
21-
)
22-
console.log('Average', +(took / total).toFixed(4), 'ms / route')
23-
24-
console.log({ memory, memoryAfter })
25-
console.log(memoryAfter - memory, 'MB memory used')
26-
27-
app.listen(3000)
28-
29-
// console.log(app.router.history)
25+
console.log(memoryAfter - memory, 'MB memory used')
26+
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "elysia",
33
"description": "Ergonomic Framework for Human",
4-
"version": "1.2.10",
4+
"version": "1.2.11-exp.1",
55
"author": {
66
"name": "saltyAom",
77
"url": "https://github.com/SaltyAom",
@@ -160,7 +160,7 @@
160160
"dependencies": {
161161
"@sinclair/typebox": "^0.34.13",
162162
"cookie": "^1.0.2",
163-
"memoirist": "^0.2.0",
163+
"memoirist": "^0.3.0",
164164
"openapi-types": "^12.1.3"
165165
},
166166
"devDependencies": {

src/compose.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,6 +2052,7 @@ export const composeGeneralHandler = (
20522052
{ asManifest = false }: { asManifest?: false } = {}
20532053
) => {
20542054
const adapter = app['~adapter'].composeGeneralHandler
2055+
app.router.http.build()
20552056
const error404 = adapter.error404(
20562057
!!app.event.request?.length,
20572058
!!app.event.error?.length

src/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export default class Elysia<
254254
http: new Memoirist<{
255255
compile: Function
256256
handler?: ComposedHandler
257-
}>(),
257+
}>({ lazy: true }),
258258
ws: new Memoirist<{
259259
compile: Function
260260
handler?: ComposedHandler
@@ -3474,9 +3474,16 @@ export default class Elysia<
34743474
if (plugin.constructor.name === 'Elysia')
34753475
return this._use(plugin.default)
34763476

3477-
throw new Error(
3478-
'Invalid plugin type. Expected Elysia instance, function, or module with "default" as Elysia instance or function that returns Elysia instance.'
3479-
)
3477+
if (plugin.constructor.name === '_Elysia')
3478+
return this._use(plugin.default)
3479+
3480+
try {
3481+
return this._use(plugin.default)
3482+
} catch {
3483+
throw new Error(
3484+
'Invalid plugin type. Expected Elysia instance, function, or module with "default" as Elysia instance or function that returns Elysia instance.'
3485+
)
3486+
}
34803487
})
34813488
.then((x) => x.compile())
34823489
)

src/utils.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,13 @@ export const mergeCookie = <const A extends Object, const B extends Object>(
9696
a: A,
9797
b: B
9898
): A & B => {
99-
// @ts-ignore
100-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
101-
const { properties: _, ...target } = a ?? {}
99+
const v = mergeDeep(Object.assign({}, a), b, {
100+
skipKeys: ['properties']
101+
}) as A & B
102102

103-
// @ts-ignore
104-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
105-
const { properties: __, ...source } = b ?? {}
103+
if ('properties' in v) delete v.properties
106104

107-
return mergeDeep(target, source) as A & B
105+
return v
108106
}
109107

110108
export const mergeObjectArray = <T extends HookContainer>(
@@ -231,7 +229,13 @@ export const mergeHook = (
231229
// customBStore[union]
232230
// )
233231

234-
const hook = Object.assign({}, a, b, {
232+
// @ts-expect-error
233+
const { resolve: resolveA, ...restA } = a ?? {}
234+
const { resolve: resolveB, ...restB } = b ?? {}
235+
236+
return {
237+
...restA,
238+
...restB,
235239
// Merge local hook first
236240
// @ts-ignore
237241
body: b?.body ?? a?.body,
@@ -261,12 +265,11 @@ export const mergeHook = (
261265
transform: mergeObjectArray(a?.transform, b?.transform),
262266
beforeHandle: mergeObjectArray(
263267
mergeObjectArray(
264-
// @ts-expect-error
265-
fnToContainer(a?.resolve, 'resolve'),
268+
fnToContainer(resolveA, 'resolve'),
266269
a?.beforeHandle
267270
),
268271
mergeObjectArray(
269-
fnToContainer(b?.resolve, 'resolve'),
272+
fnToContainer(resolveB, 'resolve'),
270273
b?.beforeHandle
271274
)
272275
),
@@ -278,11 +281,7 @@ export const mergeHook = (
278281
) as any,
279282
trace: mergeObjectArray(a?.trace, b?.trace) as any,
280283
error: mergeObjectArray(a?.error, b?.error)
281-
})
282-
283-
if (hook.resolve) delete hook.resolve
284-
285-
return hook
284+
}
286285
}
287286

288287
interface ReplaceSchemaTypeOptions {
@@ -969,7 +968,7 @@ export const mergeLifeCycle = (
969968
b: Partial<LifeCycleStore | AnyLocalHook>,
970969
checksum?: number
971970
): LifeCycleStore => {
972-
return Object.assign({}, a, b, {
971+
return {
973972
start: mergeObjectArray(
974973
a.start,
975974
injectChecksum(checksum, b?.start)
@@ -1025,7 +1024,7 @@ export const mergeLifeCycle = (
10251024
a.stop,
10261025
injectChecksum(checksum, b?.stop)
10271026
) as HookContainer<GracefulHandler<any>>[]
1028-
})
1027+
}
10291028
}
10301029

10311030
export const asHookType = (

0 commit comments

Comments
 (0)