Skip to content

AI-native design editor. Open-source Figma alternative.

Add performance benchmark for parse/export/node operations

Danila Poyarkov2f03f12210c8
diff --git a/bun.lock b/bun.lock
index 27f1867ec..d14cdcb3d 100644
--- a/bun.lock
+++ b/bun.lock
@@ -60,6 +60,7 @@
"esbuild": "^0.27.3",
"hono": "^4.12.5",
"jscpd": "^4.0.8",
+ "mitata": "^1.0.34",
"oxfmt": "^0.35.0",
"oxlint": "^1.50.0",
"oxlint-tsgolint": "^0.16.0",
@@ -1862,6 +1863,8 @@
"minisearch": ["[email protected]", "", {}, "sha512-dqT2XBYUOZOiC5t2HRnwADjhNS2cecp9u+TJRiJ1Qp/f5qjkeT5APcGPjHw+bz89Ms8Jp+cG4AlE+QZ/QnDglg=="],
+ "mitata": ["[email protected]", "", {}, "sha512-Mc3zrtNBKIMeHSCQ0XqRLo1vbdIx1wvFV9c8NJAiyho6AjNfMY8bVhbS12bwciUdd1t4rj8099CH3N3NFahaUA=="],
+
"mitt": ["[email protected]", "", {}, "sha512-vKivATfr97l2/QBCYAkXYDbrIWPM2IIKEl7YPhjCvKlG3kE2gm+uBo6nEXK3M5/Ffh/FLpKExzOQ3JJoJGFKBw=="],
"mlly": ["[email protected]", "", { "dependencies": { "acorn": "^8.15.0", "pathe": "^2.0.3", "pkg-types": "^1.3.1", "ufo": "^1.6.1" } }, "sha512-l8D9ODSRWLe2KHJSifWGwBqpTZXIXTeo8mlKjY+E2HAakaTeNpqAyBZ8GSqLzHgw4XmHmC8whvpjJNMbFZN7/g=="],
diff --git a/package.json b/package.json
index 3d4188991..17a4930ec 100644
--- a/package.json
+++ b/package.json
@@ -84,6 +84,7 @@
"esbuild": "^0.27.3",
"hono": "^4.12.5",
"jscpd": "^4.0.8",
+ "mitata": "^1.0.34",
"oxfmt": "^0.35.0",
"oxlint": "^1.50.0",
"oxlint-tsgolint": "^0.16.0",
diff --git a/tests/engine/perf.bench.ts b/tests/engine/perf.bench.ts
new file mode 100644
index 000000000..ce73d8882
--- /dev/null
+++ b/tests/engine/perf.bench.ts
@@ -0,0 +1,98 @@
+import { bench, group, run } from 'mitata'
+import { readFileSync } from 'fs'
+import { resolve } from 'path'
+
+import { parseFigFile, exportFigFile, initCodec, SceneGraph } from '@open-pencil/core'
+
+const FIXTURES = resolve(import.meta.dir, '../fixtures')
+
+const goldBuf = readFileSync(resolve(FIXTURES, 'gold-preview.fig'))
+const materialBuf = readFileSync(resolve(FIXTURES, 'material3.fig'))
+
+await initCodec()
+
+group('parseFigFile', () => {
+ bench('gold-preview.fig (537K)', async () => {
+ await parseFigFile(goldBuf.buffer.slice(0) as ArrayBuffer)
+ })
+
+ bench('material3.fig (55M)', async () => {
+ await parseFigFile(materialBuf.buffer.slice(0) as ArrayBuffer)
+ })
+})
+
+const goldGraph = await parseFigFile(goldBuf.buffer.slice(0) as ArrayBuffer)
+const materialGraph = await parseFigFile(materialBuf.buffer.slice(0) as ArrayBuffer)
+
+group('exportFigFile', () => {
+ bench('gold-preview', async () => {
+ await exportFigFile(goldGraph)
+ })
+
+ bench('material3', async () => {
+ await exportFigFile(materialGraph)
+ })
+})
+
+group('SceneGraph operations', () => {
+ bench('getAllNodes (material3)', () => {
+ const nodes = [...materialGraph.getAllNodes()]
+ void nodes.length
+ })
+
+ bench('getNode × 1000 (material3)', () => {
+ const ids = [...materialGraph.getAllNodes()].slice(0, 1000).map((n) => n.id)
+ for (const id of ids) materialGraph.getNode(id)
+ })
+
+ bench('createNode + deleteNode × 100', () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ const ids: string[] = []
+ for (let i = 0; i < 100; i++) {
+ const node = graph.createNode('RECTANGLE', page.id, {
+ name: `R${i}`,
+ width: 50,
+ height: 50
+ })
+ ids.push(node.id)
+ }
+ for (const id of ids) graph.deleteNode(id)
+ })
+})
+
+group('SceneNode memory', () => {
+ bench('create 10k nodes', () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ for (let i = 0; i < 10_000; i++) {
+ graph.createNode('RECTANGLE', page.id, {
+ name: `R${i}`,
+ width: 50,
+ height: 50
+ })
+ }
+ })
+})
+
+group('structuredClone vs copy (fills)', () => {
+ const sampleFills = [
+ { type: 'SOLID' as const, color: { r: 1, g: 0, b: 0, a: 1 }, opacity: 1, visible: true, blendMode: 'NORMAL' as const },
+ { type: 'SOLID' as const, color: { r: 0, g: 1, b: 0, a: 1 }, opacity: 0.5, visible: true, blendMode: 'NORMAL' as const }
+ ]
+
+ bench('structuredClone × 10k', () => {
+ for (let i = 0; i < 10_000; i++) structuredClone(sampleFills)
+ })
+
+ bench('manual copy × 10k', () => {
+ for (let i = 0; i < 10_000; i++) {
+ sampleFills.map((f) => ({
+ ...f,
+ color: { ...f.color }
+ }))
+ }
+ })
+})
+
+await run()