Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/lib/studio/triz-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,20 @@ describe('triz-data', () => {
// Maximum values
expect(lookupPrinciples(39, 39)).toBeDefined()
})

it('performance: completes full matrix lookup (39x39 pairs) rapidly', () => {
const start = performance.now()
let totalPrinciples = 0
for (let i = 1; i <= 39; i++) {
for (let j = 1; j <= 39; j++) {
const res = lookupPrinciples(i, j)
totalPrinciples += res.length
}
}
const duration = performance.now() - start
expect(totalPrinciples).toBeGreaterThan(0)
// 1521 lookups should take well under 50ms with O(1) Map indexing
expect(duration).toBeLessThan(50)
})
})
})
5 changes: 4 additions & 1 deletion src/lib/studio/triz-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const TRIZ_MATRIX: Record<string, number[]> = {
/** Re-exported merged contradiction matrix. */
export { TRIZ_MATRIX }

/** Pre-indexed map for O(1) principle lookups by ID. */
const PRINCIPLES_BY_ID = new Map(TRIZ_PRINCIPLES.map((p) => [p.id, p]))

/** Look up inventive principles for an improving/worsening parameter pair. */
export function lookupPrinciples(
improving: number,
Expand All @@ -36,7 +39,7 @@ export function lookupPrinciples(
const key = `${improving}-${worsening}`
const ids = TRIZ_MATRIX[key] ?? []
return ids
.map((id) => TRIZ_PRINCIPLES.find((p) => p.id === id))
.map((id) => PRINCIPLES_BY_ID.get(id))
.filter((p): p is (typeof TRIZ_PRINCIPLES)[number] => p !== undefined)
}

Loading