-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Avoid normalize calls in contains #9231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| if (substring.length === 0 || string.length < substring.length) { | ||
| return true; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also a fast path using indexOf?
| if (substring.length === 0 || string.length < substring.length) { | |
| return true; | |
| } | |
| if (substring.length === 0 || string.length < substring.length) { | |
| return true; | |
| } | |
| if (string.indexOf(substring) !== -1) { | |
| return true; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a fine optimisation. So that we know, are you running into an issue where this is slow? or it just something you noticed? or what caused you to propose the optimisation?
Edit: I read it too fast, this is going to return true for the longer substring, which I think is the opposite of what we want
Noticed code is opposite to expectation
|
Actually, did some more reading on normalize, two un-normalized strings can be different lengths but still be equal, so I don't think we can do this fast path at all Thanks for the thoughts, we can open an issue for more discussion if you don't mind still answering
|
I wasn't running into any specific issue with it, I just noticed that it might be an optimization while implementing an fuzzyContains extension of the useFilter hook. If you found that it would cause false positives then I wouldn't advocate for it 😄 export function useFilter(options: FilterOptions = {}): Filter {
const { threshold = 2, maxLength = 256, sensitivity = "base", ...collatorOptions } = options;
const ariaOpts = { sensitivity, ...collatorOptions };
const filter = useFilterAria(ariaOpts);
const collator = useCollator(ariaOpts);
const normalize = useMemo(
() => (str: string) => normalizeForCollation(str, sensitivity, collator),
[collator, sensitivity]
);
const fuzzyContains = useCallback(
(text: string, query: string): boolean => {
if (!query) return true;
if (!text) return false;
if (text.length > maxLength || query.length > maxLength) return false;
if (filter.contains(text, query)) return true;
if (Math.abs(text.length - query.length) > threshold) return false;
const textNorm = normalize(text);
const queryNorm = normalize(query);
if (textNorm === queryNorm) return true;
if (Math.abs(textNorm.length - queryNorm.length) > threshold) return false;
return distance(textNorm, queryNorm) <= threshold;
},
[filter.contains, normalize, threshold, maxLength]
);
return {
...filter,
fuzzyContains,
};
} |
Closes
✅ Pull Request Checklist:
📝 Test Instructions:
🧢 Your Project: