Skip to content

Commit c4b9399

Browse files
KyleAMathewsclaude
andauthored
fix(db): compile filter expression once instead of on every invocation (#954)
* fix(db): compile filter expression once instead of on every invocation Previously, `createFilterFunctionFromExpression` was calling `compileSingleRowExpression(expression)` inside the returned filter function, causing the expression to be recompiled for every item being filtered. This resulted in severe performance degradation when filtering large collections, as `normalizeValue` and other compilation functions were called thousands of times unnecessarily. Move the compilation outside the returned function so it happens once when the filter function is created. * chore: add changeset for filter expression compile fix --------- Co-authored-by: Claude <[email protected]>
1 parent e12514a commit c4b9399

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@tanstack/db": patch
3+
---
4+
5+
fix(db): compile filter expression once in createFilterFunctionFromExpression
6+
7+
Fixed a performance issue in `createFilterFunctionFromExpression` where the expression was being recompiled on every filter call. This only affected realtime change event filtering for pushed-down predicates at the collection level when using orderBy + limit. The core query engine was not affected as it already compiled predicates once.

packages/db/src/collection/change-events.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,11 @@ export function createFilterFunction<T extends object>(
221221
export function createFilterFunctionFromExpression<T extends object>(
222222
expression: BasicExpression<boolean>
223223
): (item: T) => boolean {
224+
// Compile expression once when filter function is created, not on every invocation
225+
const evaluator = compileSingleRowExpression(expression)
226+
224227
return (item: T): boolean => {
225228
try {
226-
const evaluator = compileSingleRowExpression(expression)
227229
const result = evaluator(item as Record<string, unknown>)
228230
return toBooleanPredicate(result)
229231
} catch {

0 commit comments

Comments
 (0)