-
Notifications
You must be signed in to change notification settings - Fork 1.6k
perf: build the js-search TF-IDF index lazily instead of on mount #8023
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Bryandero98
wants to merge
3
commits into
layer5io:master
Choose a base branch
from
Bryandero98:perf/lazy-search-index-usedatalist
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,44 +1,63 @@ | ||
| import * as JsSearch from "js-search"; | ||
| import { useState, useEffect } from "react"; | ||
| import { useState, useEffect, useRef } from "react"; | ||
| const useDataList = ( | ||
| data, | ||
| setSearchQuery, | ||
| searchQuery, | ||
| paramsIndex, | ||
| paramSearch | ||
| paramSearch, | ||
| ) => { | ||
| const [dataList, setDataList] = useState(data); | ||
| const [search, setSearch] = useState([]); | ||
| const [searchResults, setSearchResults] = useState([]); | ||
| const [isLoading, setIsLoading] = useState(true); | ||
| const queryResults = searchQuery ? searchResults : dataList; | ||
| useEffect(() => { | ||
| rebuildIndex(); | ||
| }, [dataList]); | ||
|
|
||
| // The TF-IDF index is the expensive part of a search (it walks and | ||
| // tokenizes every document up front) - built lazily on the first real | ||
| // keystroke instead of eagerly on mount, since most renders of this hook | ||
| // (embedded previews, limited-count sections, visitors who never search) | ||
| // never need it at all. Invalidated whenever the underlying dataset | ||
| // changes so a stale index is never served. | ||
| const searchIndexRef = useRef(null); | ||
|
|
||
| useEffect(() => { | ||
| rebuildIndex(); | ||
| }, []); | ||
| searchIndexRef.current = null; | ||
| // An active query's results were built from the old dataList - recompute | ||
| // them against the new one immediately instead of waiting for the next | ||
| // keystroke, otherwise queryResults keeps showing stale matches. | ||
| if (searchQuery) { | ||
| setSearchResults(getSearchIndex().search(searchQuery)); | ||
| } else { | ||
| setSearchResults([]); | ||
| } | ||
| }, [dataList]); | ||
|
|
||
| const rebuildIndex = () => { | ||
| const dataToSearch = new JsSearch.Search(paramSearch); | ||
| dataToSearch.indexStrategy = new JsSearch.PrefixIndexStrategy(); | ||
| dataToSearch.sanitizer = new JsSearch.LowerCaseSanitizer(); | ||
| dataToSearch.searchIndex = new JsSearch.TfIdfSearchIndex(paramSearch); | ||
| dataToSearch.addIndex(paramsIndex); | ||
| dataToSearch.addIndex("body"); | ||
| dataToSearch.addDocuments(dataList); | ||
| setSearch(dataToSearch); | ||
| setIsLoading(false); | ||
| const getSearchIndex = () => { | ||
| if (!searchIndexRef.current) { | ||
| const dataToSearch = new JsSearch.Search(paramSearch); | ||
| dataToSearch.indexStrategy = new JsSearch.PrefixIndexStrategy(); | ||
| dataToSearch.sanitizer = new JsSearch.LowerCaseSanitizer(); | ||
| dataToSearch.searchIndex = new JsSearch.TfIdfSearchIndex(paramSearch); | ||
| dataToSearch.addIndex(paramsIndex); | ||
| dataToSearch.addIndex("body"); | ||
| dataToSearch.addDocuments(dataList); | ||
| searchIndexRef.current = dataToSearch; | ||
| } | ||
| return searchIndexRef.current; | ||
| }; | ||
|
|
||
| const searchData = (e) => { | ||
| const queryResult = search.search(e.target.value); | ||
| setSearchQuery(e.target.value.trim()); | ||
| setSearchResults(queryResult); | ||
| const trimmedQuery = e.target.value.trim(); | ||
| setSearchQuery(trimmedQuery); | ||
| // A blank query has nothing to search - skip it so an empty keystroke | ||
| // (or clearing the field) never forces the lazy index to build. | ||
| if (!trimmedQuery) { | ||
| setSearchResults([]); | ||
| return; | ||
| } | ||
| setSearchResults(getSearchIndex().search(trimmedQuery)); | ||
| }; | ||
|
|
||
| return { queryResults, searchData, setDataList,dataList }; | ||
| return { queryResults, searchData, setDataList, dataList }; | ||
| }; | ||
|
|
||
| export default useDataList; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.