Skip to content

Commit

Permalink
perf: rewrites 'getCommonPrefix' in radix tree
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva committed Oct 7, 2024
1 parent 0dedb2b commit 8a381e3
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions packages/orama/src/trees/radix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,16 +344,13 @@ export class RadixNode {
}

private static getCommonPrefix(a: string, b: string): string {
let commonPrefix = ''
const len = Math.min(a.length, b.length)
for (let i = 0; i < len; i++) {
if (a[i] !== b[i]) {
return commonPrefix
}
commonPrefix += a[i]
let i = 0
while (i < len && a.charCodeAt(i) === b.charCodeAt(i)) {
i++
}
return commonPrefix
}
return a.slice(0, i)
}

public toJSON(): object {
return {
Expand Down

0 comments on commit 8a381e3

Please sign in to comment.