Skip to content

Avoid args.splice in format string processing — build new array #8

@alanzabihi

Description

@alanzabihi

Hypothesis

The format string processing loop (common.js lines 90-107) uses args.splice(index, 1) to remove consumed arguments inline. splice is O(n) per call, shifting all remaining elements. For messages with multiple format specifiers (%s %d etc.), this compounds. Building the final args array without mutation should be faster.

Rationale

Current code splices args in-place as formatters consume arguments. For a message like log('req %s took %dms', url, time), splice runs twice, each time shifting remaining elements.

Alternative approach: iterate the format string, collect the formatted output and unconsumed args into a new array. No mutation, no element shifting. The new array can be pre-allocated or built with push().

Scope

  • src/common.js lines 90-107: rewrite the formatter loop to build a new args array instead of splicing the existing one

Expected impact

Small to moderate. The benchmark has one call type with formatters (%s/%d with 2 specifiers) and one with %O (1 specifier). splice overhead is proportional to remaining args length which is small (2-3 elements). But the allocation pattern change may also help V8 optimization.

Risks

Must preserve exact semantics: formatters receive the correct arg value, unconsumed args appear in the output, %% escaping works, and the final args array has the right shape for formatArgs and logFn.apply.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions