For code that executes on every request or in other hot paths (router matching, context/response creation, param decoding, JSX stringification, etc.), prioritize measured performance over assumed performance, and avoid redundant work.
Concretely:
mitata or simple timing loops) across the runtimes you target (Node, Bun, Deno), since relative performance of built-ins like .at(-1) vs arr[arr.length - 1], in vs Object.keys().includes(), or new Response() with/without a status option can differ significantly between engines..flat() unconditionally when most inputs don’t need it; prefer native fast paths like Response.json() over manual JSON.stringify + new Response where equivalent./%/.test() before an expensive decode, or a Content-Length` threshold check before compressing) to skip expensive work when it isn’t needed, even if it adds a small branch.Example: prefer hoisting a regex instead of recompiling it per call:
// Bad: regex recompiled on every call
const parseAcceptValue = (value: string) => {
const parts = value.split(/;(?=(?:(?:[^"]*"){2})*[^"]*$)/)
// ...
}
// Good: regex compiled once
const SPLIT_REGEX = /;(?=(?:(?:[^"]*"){2})*[^"]*$)/
const parseAcceptValue = (value: string) => {
const parts = value.split(SPLIT_REGEX)
// ...
}