Skip to content
Merged
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
43 changes: 23 additions & 20 deletions src/__tests__/ics-parser-performance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,27 +106,30 @@ END:VCALENDAR`;
});

describe("Caching Performance", () => {
test("should benefit from caching on repeated parsing", () => {
test("should serve repeated parsing from the cache", () => {
const content = createMockIcsContent(100);

// First parse (no cache)
const startTime1 = performance.now();
const result1 = IcsParser.parse(content, testSource);
const endTime1 = performance.now();
const firstParseTime = endTime1 - startTime1;

// Second parse (with cache)
const startTime2 = performance.now();
const result2 = IcsParser.parse(content, testSource);
const endTime2 = performance.now();
const secondParseTime = endTime2 - startTime2;

expect(result1.events).toHaveLength(100);
expect(result2.events).toHaveLength(100);
expect(secondParseTime).toBeLessThan(firstParseTime * 0.5); // Cache should be at least 50% faster

console.log(`First parse: ${firstParseTime.toFixed(2)}ms, Cached parse: ${secondParseTime.toFixed(2)}ms`);
console.log(`Cache speedup: ${(firstParseTime / secondParseTime).toFixed(2)}x`);
// Check the cache directly: timing a ~1 ms parse against a cache hit
// is dominated by CI noise (GC pauses), and the hit still hashes
// the whole content.
const unfoldLines = jest.spyOn(
IcsParser as unknown as { unfoldLines: (lines: string[]) => string[] },
"unfoldLines",
);
try {
const result1 = IcsParser.parse(content, testSource);
const result2 = IcsParser.parse(content, testSource);

expect(result1.events).toHaveLength(100);
expect(result2.events).toHaveLength(100);
// Only the first call parsed the content.
expect(unfoldLines).toHaveBeenCalledTimes(1);
expect(IcsParser.getCacheStats().size).toBe(1);
// The cached result is copied, so callers can't mutate the cache.
expect(result2.events).toEqual(result1.events);
expect(result2.events[0]).not.toBe(result1.events[0]);
} finally {
unfoldLines.mockRestore();
}
});

test("should manage cache size properly", () => {
Expand Down
Loading