perf(cmd-j): scan phrase word boundaries monotonically (#20282)

This commit is contained in:
Neil
2026-09-12 18:36:47 -07:00
committed by GitHub
parent 824dc5353a
commit 25d6cd75eb
@@ -34,10 +34,23 @@ function phrasePlacement(field: PaletteIndexedField, normalizedQuery: string): n
if (text.startsWith(normalizedQuery)) {
return 0
}
for (const word of field.words) {
if (text.startsWith(normalizedQuery, word.start)) {
// Merge the ordered occurrence and word-start streams: occurrences only count at word
// starts, so each side advances monotonically and never rescans the other.
let index = text.indexOf(normalizedQuery, 1)
let wordIndex = 0
while (index !== -1) {
let wordStart = field.words[wordIndex]?.start
while (wordStart !== undefined && wordStart < index) {
wordIndex += 1
wordStart = field.words[wordIndex]?.start
}
if (wordStart === undefined) {
break
}
if (wordStart === index) {
return 1
}
index = text.indexOf(normalizedQuery, wordStart)
}
return 2
}