Skip to content
Myr

perf(request): allocate `#validatedData` lazily (#5175)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The constructor allocated `#validatedData = {}` on every `HonoRequest`, so every request paid for the validator feature whether or not any validator ran. It is now created on the first `addValidatedData()` call. `valid()` reads through optional chaining and still returns `undefined` for targets that were never validated, as before.

authored by Igor Savin · committed by Yusuke Wada · parent 3fbbd96 · Browse files · 1 file+3 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+3 added−4 removed

  • other:+3 added−4 removed

1 file+3 added−4 removed

modified:src/request.ts+3 added-4 removed
46 unmodified lines
47 * ```48 */49 raw: Request5051 #validatedData: { [K in keyof ValidationTargets]?: {} } // Short name of validatedData51 #validatedData: { [K in keyof ValidationTargets]?: {} } | undefined // Short name of validatedData52 #matchResult: Result<[unknown, RouterRoute]>53 routeIndex: number = 054 /**55 * `.path` can get the pathname of the request.
17 unmodified lines
73 ) {74 this.raw = request75 this.path = path76 this.#matchResult = matchResult77 this.#validatedData = {}77 }7879 /**80 * `.req.param()` gets the path parameters.
252 unmodified lines
333 * @param target - The target of the validation.334 * @param data - The validated data to add.335 */336 addValidatedData(target: keyof ValidationTargets, data: {}) {338 this.#validatedData[target] = data337 ;(this.#validatedData ??= {})[target] = data338 }339340 /**341 * Gets validated data from the request.
4 unmodified lines
346 * @see https://hono.dev/docs/api/request#valid347 */348 valid<T extends keyof I & keyof ValidationTargets>(target: T): InputToDataByTarget<I, T>349 valid(target: keyof ValidationTargets) {351 return this.#validatedData[target] as unknown350 return this.#validatedData?.[target] as unknown351 }352353 /**354 * `.url()` can get the request url strings.
156 unmodified lines