Skip to content

Reduce allocations in compile loop by reusing context object #46

@punkpeye

Description

@punkpeye

Initial checklist

Problem

In the compile function, a new object is created via Object.assign for every event in the token stream:

while (++index < events.length) {
  const handler = config[events[index][0]]

  if (own.call(handler, events[index][1].type)) {
    handler[events[index][1].type].call(
      Object.assign(
        {sliceSerialize: events[index][2].sliceSerialize},
        context
      ),
      events[index][1]
    )
  }
}

This can result in thousands of object allocations. These short-lived objects create GC pressure, which shows up as garbage collection pauses in CPU profiles.

Current solutions

N/A

Proposed solutions

Since sliceSerialize is the only property that varies per event, we can reuse a single context object and mutate just that property.

Metadata

Metadata

Assignees

No one assigned

    Labels

    🤞 phase/openPost is being triaged manually

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions