Hypothesis
The enabled path does args[0] = createDebug.coerce(args[0]) (common.js line 82). This requires a property lookup on createDebug for every enabled call. Since coerce never changes, capturing it in a local variable at factory scope eliminates the repeated property lookup.
Rationale
createDebug.coerce is set once at module load (common.js line 10) and never modified. But every enabled call does a property lookup: createDebug.coerce. In V8, property lookups on objects with many properties (createDebug has ~15) are slower than local variable access.
Similarly, createDebug.formatters (line 97), createDebug.formatArgs (line 110), and createDebug.log (line 112) are all looked up per call. Caching these in local variables at setup() scope would eliminate 4+ property lookups per enabled call.
Scope
- src/common.js setup(): cache coerce, formatters, formatArgs, log as local const variables
- Update references in the enabled path to use locals
Expected impact
Small to moderate. 4 property lookups eliminated per enabled call x 200K calls. V8 optimizes local variable access much better than property lookups.
Risks
If any of these are modified after setup (e.g. formatters can be extended by users), the cached reference must be the object itself (not a copy), so mutations are still visible. formatters is an object reference, so caching the reference is safe. coerce is never overridden.
Hypothesis
The enabled path does args[0] = createDebug.coerce(args[0]) (common.js line 82). This requires a property lookup on createDebug for every enabled call. Since coerce never changes, capturing it in a local variable at factory scope eliminates the repeated property lookup.
Rationale
createDebug.coerce is set once at module load (common.js line 10) and never modified. But every enabled call does a property lookup: createDebug.coerce. In V8, property lookups on objects with many properties (createDebug has ~15) are slower than local variable access.
Similarly, createDebug.formatters (line 97), createDebug.formatArgs (line 110), and createDebug.log (line 112) are all looked up per call. Caching these in local variables at setup() scope would eliminate 4+ property lookups per enabled call.
Scope
Expected impact
Small to moderate. 4 property lookups eliminated per enabled call x 200K calls. V8 optimizes local variable access much better than property lookups.
Risks
If any of these are modified after setup (e.g. formatters can be extended by users), the cached reference must be the object itself (not a copy), so mutations are still visible. formatters is an object reference, so caching the reference is safe. coerce is never overridden.