Skip to content

Commit a759695

Browse files
committed
perf: optimize infinite loop detection test
Use more efficient nested structure creation instead of creating 1 million individual objects
1 parent 73f9d43 commit a759695

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

test/purl-edge-cases.test.mts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ SOFTWARE.
2323
import { describe, expect, it } from 'vitest'
2424

2525
import { createTestFunction } from './utils/test-helpers.mjs'
26-
import { LOOP_SENTINEL } from '../src/constants.js'
26+
// import { LOOP_SENTINEL } from '../src/constants.js' -- No longer needed after optimization
2727
import {
2828
encodeComponent,
2929
encodeNamespace,
@@ -1158,15 +1158,28 @@ describe('Edge cases and additional coverage', () => {
11581158

11591159
// Test recursiveFreeze infinite loop detection
11601160
it('should detect infinite loops in recursiveFreeze', () => {
1161-
// Create a large array to trigger loop detection
1162-
const obj = { arr: [] }
1163-
// Add exactly LOOP_SENTINEL items to trigger the check
1164-
for (let i = 0; i < LOOP_SENTINEL; i++) {
1165-
;(obj.arr as any).push({ value: i })
1161+
// Create a structure that will exceed LOOP_SENTINEL when traversed
1162+
// Use nested objects to reach the limit more efficiently
1163+
const createDeepStructure = () => {
1164+
const depth = 1000
1165+
// depth * width > 1,000,000 (LOOP_SENTINEL)
1166+
const width = 1001
1167+
const root: any = { children: [] }
1168+
1169+
for (let i = 0; i < width; i++) {
1170+
let current = root
1171+
for (let j = 0; j < depth; j++) {
1172+
const child = { value: `${i}-${j}`, children: [] }
1173+
current.children.push(child)
1174+
current = child
1175+
}
1176+
}
1177+
return root
11661178
}
11671179

1180+
const largeGraph = createDeepStructure()
11681181
// This should throw when hitting the sentinel
1169-
expect(() => recursiveFreeze(obj)).toThrow(/Object graph too large/)
1182+
expect(() => recursiveFreeze(largeGraph)).toThrow(/Object graph too large/)
11701183
})
11711184

11721185
// Test purl-component functions

0 commit comments

Comments
 (0)