Skip to content

Optimize object literals #974

Open
Open

Description

@bnoordhuis
function o(x,y,z){ return {x,y,z} }

Produces bytecode that sets the fields 1-by-1:

;; function o(x, y, z) { return {x, y, z} }

        object         
        get_arg0        0    ; x
        define_field    x
        get_arg1        1    ; y
        define_field    y
        get_arg2        2    ; z
        define_field    z
        return         

It should be more efficient to use an "object template" that describes what properties the object has:

;; function o(x, y, z) { return {x, y, z} }

        get_arg0        0    ; x
        get_arg1        1    ; y
        get_arg2        2    ; z
        define_object <id>   ; ( x y z -- o )
        return         

Benefits:

  • reduces bytecode size
  • object layout is known upfront, don't have to update JSShape repeatedly

Activity

  1. bnoordhuis commented on Sep 8, 2026

    @bnoordhuis
    ContributorAuthor

    I experimented with this:

    • storing the object template out of band (like on JSFunctionBytecode) makes it hard-to-impossible to (de)serialize function objects with JS_WriteObject

    • storing it in the bytecode stream itself turns define_object into a variable-length opcode (the only one we have) and dealing with that gets finicky. Not impossible but definitely finicky.

    There's no big difference in performance in my limited testing, although I don't pre-allocate the final JSShape yet. It definitely shrinks the bytecode for large object literals though, but how common are those?

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

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions