Skip to content
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

chore: avoid recalculating lengths in some for-loops #553

Merged
Merged
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
15 changes: 10 additions & 5 deletions packages/orama/src/trees/radix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ export function create (end = false, subWord = '', key = ''): Node {
}

export function insert (root: Node, word: string, docId: InternalDocumentID) {
for (let i = 0; i < word.length; i++) {
const wordLength = word.length
for (let i = 0; i < wordLength; i++) {
const currentCharacter = word[i]
const wordAtIndex = word.substring(i)
const rootChildCurrentChar = root.c[currentCharacter]
Expand Down Expand Up @@ -270,7 +271,8 @@ export function find (root: Node, { term, exact, tolerance }: FindParams): FindR
_findLevenshtein(root, term, 0, tolerance || 0, tolerance, output)
return output
} else {
for (let i = 0; i < term.length; i++) {
const termLength = term.length
for (let i = 0; i < termLength; i++) {
const character = term[i]
if (character in root.c) {
const rootChildCurrentChar = root.c[character]
Expand Down Expand Up @@ -306,7 +308,8 @@ export function find (root: Node, { term, exact, tolerance }: FindParams): FindR
}

export function contains (root: Node, term: string): boolean {
for (let i = 0; i < term.length; i++) {
const termLength = term.length
for (let i = 0; i < termLength; i++) {
const character = term[i]

if (character in root.c) {
Expand All @@ -333,7 +336,8 @@ export function removeWord (root: Node, term: string): boolean {
return false
}

for (let i = 0; i < term.length; i++) {
const termLength = term.length
for (let i = 0; i < termLength; i++) {
const character = term[i]
const parent = root
if (character in root.c) {
Expand All @@ -357,7 +361,8 @@ export function removeDocumentByWord (root: Node, term: string, docID: InternalD
return true
}

for (let i = 0; i < term.length; i++) {
const termLength = term.length
for (let i = 0; i < termLength; i++) {
const character = term[i]
if (character in root.c) {
const rootChildCurrentChar = root.c[character]
Expand Down