Skip to content

perf: pre-compile hot-path JS expressions in QuickJS Init() #1186

Description

@zeroedin

Description

CallHook calls Eval("__hooks[__callHookName](__callInput)") per page per hook — parsing and compiling the same JS expression 820+ times per hook invocation. setPayloadFrontMatter and setPayloadTOC each call Eval("Object.defineProperty(...)") per page for the lazy getter setup.

Eval lexes, parses, and compiles the source string on every call. For a static expression called 820 times, that's 819 redundant compilations.

Fix

Pre-compile static expressions as named functions during Init():

function __callHookByName(name, input) {
  return __hooks[name](input);
}
function __installLazyFM(target, jsonStr) {
  Object.defineProperty(target, 'frontMatter', { get: ..., set: ..., configurable: true });
}
function __installLazyTOC(target, jsonStr) {
  Object.defineProperty(target, 'toc', { get: ..., set: ..., configurable: true });
}

Call via InvokeJS("__callHookByName", nameVal, input) instead of Eval(...). InvokeJS calls the pre-compiled function directly — no parse, no compile.

Measured impact

820-page RHDS benchmark: ~200ms reduction in post-render hooks. Small individually, but it compounds with the other fast-path optimizations.

Scope

Refs #1185, #1180

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions