Skip to content

Compiling a wide object literal is superlinear in key count (1,000 keys = 945 s CPU) #9789

Description

@proggeramlug

Compile time for an object literal grows far faster than its key count. Runtime is not the problem — the compiled program is much faster than node (quiet host, 5,000 keys: node 1.42 s, perry 0.00 s). The cost is entirely in compilation, and literals this wide appear in generated code and bundled config.

Measured on the dev box with cache-busting unique content and user CPU time:

Shape A — literal + 97 property reads + Object.keys:

keys compile, user CPU
1 1.05 s
2,000 112.31 s

Shape B — literal + Object.keys only, no reads:

keys compile, user CPU vs previous
250 19.71 s
500 120.95 s x6.1 for x2 keys
1,000 944.68 s x7.8 for x2 keys

Shape B is roughly n^2.6 to n^3.

Two caveats for whoever picks this up

  1. The shapes contradict. 2,000 keys in shape A cost 112 s, while 1,000 keys in shape B cost 945 s. Removing the property reads appears to make compilation dramatically worse, which would itself be the finding — or it is measurement error. Not resolved.
  2. Load. Every number was taken at load average 77-104. user CPU is far less load-sensitive than wall clock, which is why it is quoted, but the contradiction above means the exponent is not established. Re-run both shapes on a quiet host before quoting a complexity class.

Measurement notes

The object cache makes a repeat compile of the same source meaningless — the first attempt at this measurement was a cache hit and read as "no problem". Generate unique content per run.

Done when

Compile time is roughly linear in key count.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions