Skip to content

Commit 268888b

Browse files
committed
suggestionList: convert strings to arrays before calculation
1 parent 4c10844 commit 268888b

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/jsutils/suggestionList.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ export default function suggestionList(
4242
class LexicalDistance {
4343
_input: string;
4444
_inputLowerCase: string;
45+
_inputArray: Array<number>;
4546
_rows: [Array<number>, Array<number>, Array<number>];
4647

4748
constructor(input: string) {
4849
this._input = input;
4950
this._inputLowerCase = input.toLowerCase();
51+
this._inputArray = stringToArray(this._inputLowerCase);
5052

5153
this._rows = [
5254
new Array(input.length + 1).fill(0),
@@ -67,13 +69,14 @@ class LexicalDistance {
6769
return 1;
6870
}
6971

70-
let a = optionLowerCase;
71-
let b = this._inputLowerCase;
72+
let a = stringToArray(optionLowerCase);
73+
let b = this._inputArray;
74+
7275
if (a.length < b.length) {
76+
const tmp = a;
7377
a = b;
74-
b = optionLowerCase;
78+
b = tmp;
7579
}
76-
7780
const aLength = a.length;
7881
const bLength = b.length;
7982

@@ -123,3 +126,12 @@ class LexicalDistance {
123126
return distance <= threshold ? distance : undefined;
124127
}
125128
}
129+
130+
function stringToArray(str) {
131+
const strLength = str.length;
132+
const array = new Array(strLength);
133+
for (let i = 0; i < strLength; ++i) {
134+
array[i] = str.charCodeAt(i);
135+
}
136+
return array;
137+
}

0 commit comments

Comments
 (0)