Skip to content

Improve match-sorter-util performance by replacing 4 string compariso… #6020

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions packages/match-sorter-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,26 @@ function getMatchRanking<TItem>(
testString = testString.toLowerCase()
stringToRank = stringToRank.toLowerCase()

// Use indexOf to check for equality/includes
const indexOfStringToRankInTestString = testString.indexOf(stringToRank)

// case insensitive equals
if (testString === stringToRank) {
if (testString.length === stringToRank.length && indexOfStringToRankInTestString === 0) {
return rankings.EQUAL
}

// starts with
if (testString.startsWith(stringToRank)) {
if (indexOfStringToRankInTestString === 0) {
return rankings.STARTS_WITH
}

// word starts with
if (testString.includes(` ${stringToRank}`)) {
if (indexOfStringToRankInTestString > 0 && testString[indexOfStringToRankInTestString-1] === ' ') {
return rankings.WORD_STARTS_WITH
}

// contains
if (testString.includes(stringToRank)) {
if (indexOfStringToRankInTestString > 0) {
return rankings.CONTAINS
} else if (stringToRank.length === 1) {
// If the only character in the given stringToRank
Expand Down