From 956849f30f420e6a8c0ac43168a91569e7cd2767 Mon Sep 17 00:00:00 2001 From: Orion Reed Date: Sat, 7 Dec 2024 01:30:00 -0500 Subject: [PATCH] tiny benchmark util --- src/__tests__/TransformDOMRect.test.ts | 58 ++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/__tests__/TransformDOMRect.test.ts b/src/__tests__/TransformDOMRect.test.ts index 0a51da8..8b11f51 100644 --- a/src/__tests__/TransformDOMRect.test.ts +++ b/src/__tests__/TransformDOMRect.test.ts @@ -145,3 +145,61 @@ describe('TransformDOMRectReadonly', () => { expect(rect.height).toBe(50); }); }); + +describe('Performance Tests', () => { + test('matrix operations performance', () => { + const rect = new TransformDOMRect({ + x: 10, + y: 20, + width: 100, + height: 50, + rotation: Math.PI / 4, + }); + const point = { x: 25, y: 25 }; + + measurePerformance('toLocalSpace()', 1_000_000, () => { + rect.toLocalSpace(point); + }); + + measurePerformance('toParentSpace()', 1_000_000, () => { + rect.toParentSpace(point); + }); + }); + + test('vertices calculation performance', () => { + const rect = new TransformDOMRect({ + x: 10, + y: 20, + width: 100, + height: 50, + rotation: Math.PI / 6, + }); + + measurePerformance('getBounds()', 1_000_000, () => { + rect.getBounds(); + }); + + measurePerformance('vertices()', 1_000_000, () => { + rect.vertices(); + }); + + // Note: We might want to add an assertion here if needed + }); +}); + +function measurePerformance(label: string, iterations: number, fn: () => void): void { + const start = Bun.nanoseconds(); + + for (let i = 0; i < iterations; i++) { + fn(); + } + + const end = Bun.nanoseconds(); + const timeInNs = end - start; + const timeInMs = timeInNs / 1_000_000; + const opsPerMs = iterations / timeInMs; + + const formattedSpeed = opsPerMs.toFixed(2); + + console.log(' \x1b[36m%s\x1b[0m \x1b[1m%s\x1b[0m \x1b[33m%s\x1b[0m', `${label}:`, formattedSpeed, 'ops/ms'); +}