From 3ede82811a7d7652ed3189ee7aa3ce36b48b055d Mon Sep 17 00:00:00 2001 From: Vaclav Vancura Date: Sat, 28 Mar 2026 14:24:32 +0100 Subject: [PATCH] refactor(utils): structure Rect2i benchmarks with region comments Organized `Rect2i.bench.ts` using labeled region comments for improved readability. Added constants and clearly separated benchmark suites for creation, collision detection, intersections, and property access. Co-Authored-By: Claude Signed-off-by: Vaclav Vancura --- src/utils/Rect2i.bench.ts | 112 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/utils/Rect2i.bench.ts diff --git a/src/utils/Rect2i.bench.ts b/src/utils/Rect2i.bench.ts new file mode 100644 index 0000000..3c2a1ed --- /dev/null +++ b/src/utils/Rect2i.bench.ts @@ -0,0 +1,112 @@ +// #region Imports + +import { bench, describe } from 'vitest'; + +import { Rect2i } from './Rect2i'; +import { Vector2i } from './Vector2i'; + +// #endregion + +// #region Constants + +const BASE_X = 10; +const BASE_Y = 20; +const BASE_WIDTH = 64; +const BASE_HEIGHT = 48; +const propertyAccessSink = { value: 0 }; +const BENCH_OPTIONS = { + iterations: 500, + time: 100, + warmupTime: 25, + warmupIterations: 50, +}; + +// #endregion + +// #region Benchmark Suites + +describe('Rect2i creation benchmarks', () => { + const source = Rect2i.fromValuesUnchecked(BASE_X, BASE_Y, BASE_WIDTH, BASE_HEIGHT); + const mutable = new Rect2i(); + + bench( + 'new Rect2i()', + () => { + new Rect2i(BASE_X, BASE_Y, BASE_WIDTH, BASE_HEIGHT); + }, + BENCH_OPTIONS, + ); + + bench( + 'clone()', + () => { + source.clone(); + }, + BENCH_OPTIONS, + ); + + bench( + 'set()', + () => { + mutable.set(BASE_X, BASE_Y, BASE_WIDTH, BASE_HEIGHT); + }, + BENCH_OPTIONS, + ); +}); + +describe('Rect2i collision benchmarks', () => { + const rect = Rect2i.fromValuesUnchecked(BASE_X, BASE_Y, BASE_WIDTH, BASE_HEIGHT); + const point = Vector2i.fromXYUnchecked(24, 32); + const overlapping = Rect2i.fromValuesUnchecked(30, 36, 20, 18); + + bench( + 'contains(Vector2i)', + () => { + rect.contains(point); + }, + BENCH_OPTIONS, + ); + + bench( + 'containsXY()', + () => { + rect.containsXY(24, 32); + }, + BENCH_OPTIONS, + ); + + bench( + 'intersects(Rect2i)', + () => { + rect.intersects(overlapping); + }, + BENCH_OPTIONS, + ); +}); + +describe('Rect2i intersection benchmarks', () => { + const rect = Rect2i.fromValuesUnchecked(BASE_X, BASE_Y, BASE_WIDTH, BASE_HEIGHT); + const overlapping = Rect2i.fromValuesUnchecked(30, 36, 20, 18); + + bench( + 'intersection()', + () => { + rect.intersection(overlapping); + }, + BENCH_OPTIONS, + ); +}); + +describe('Rect2i property access benchmarks', () => { + const rect = Rect2i.fromValuesUnchecked(BASE_X, BASE_Y, BASE_WIDTH, BASE_HEIGHT); + + bench( + 'property access x/y/width/height', + () => { + propertyAccessSink.value = rect.x + rect.y + rect.width + rect.height; + }, + BENCH_OPTIONS, + ); +}); + +// #endregion -- 2.54.0