openpencil/packages/fig/tests/document/node-shape.test.ts
Danila Poyarkov 9c28c13fdf
perf(scene-graph): give imported nodes the shape every node shares (#903)
* perf(scene-graph): give imported nodes the shape every node shares

createDefaultNode had no default for booleanOperation, and the .fig
importer sets that key on every node. A key outside an object's initial
shape turns it into a JavaScriptCore dictionary, so every imported node
had its own structure and dictionary property storage. Fully loading
material3.fig took 3.7 GB RSS in Bun; with the default it takes 2.8 GB,
with 0.5 GB less JS heap and 0.46 GB less memory outside it. V8 keeps
these objects fast either way.

The defaults now must name every SceneNode field, which the compiler
enforces, and a test checks that imported nodes carry exactly the
default fields. booleanOperation states the explicit undefined the
importer already stores.

* fix(scene-graph): require booleanOperation and compare node keys in order

Every SceneNode field is required except booleanOperation, so a typed
producer could still omit it and build a node with a different shape.
It is now required, with undefined still meaning no operation. The
node-shape test compares keys in insertion order, since JavaScriptCore
lays out the same keys added in another order as a different shape.

The changelog entry no longer quotes the Bun measurement as figures for
the macOS app and Safari, and records the type change as breaking.
2026-10-05 17:14:12 +00:00

24 lines
1.2 KiB
TypeScript

import { expect, test } from 'bun:test'
import { readFixtureArrayBuffer } from '#fig-tests/helpers/fig-fixtures'
import { createFigDocumentSession } from '@open-pencil/fig'
import { createDefaultNode } from '@open-pencil/scene-graph/node-defaults'
// Imported nodes must keep the shape every node is created with: a key outside it turns each
// node into a JavaScriptCore dictionary, which made opening large files in WebKit take about a
// fifth more memory.
test('imported nodes carry exactly the default scene node fields', () => {
const session = createFigDocumentSession(readFixtureArrayBuffer('gold-preview.fig'))
for (const page of session.pages) if (!page.internalOnly) session.loadPage(page.id)
// Insertion order matters too: JavaScriptCore lays out the same keys added in another order
// as a different shape.
const fields = Object.keys(createDefaultNode(() => 'node', 'FRAME'))
const shapes = new Map<string, string>()
for (const node of session.graph.nodes.values()) {
const keys = Object.keys(node).join(',')
if (!shapes.has(keys)) shapes.set(keys, node.id)
}
expect([...shapes.keys()].map((keys) => keys.split(','))).toEqual([fields])
})