padolsey / relevancy.js

:bar_chart: Sort/Weigh strings by relevance
286 stars 8 forks source link

Efficiency in _generateSubjectRegex #5

Open sunnypp opened 5 years ago

sunnypp commented 5 years ago

I don't know if this is actively maintained.

I looked into the code and see _generateSubjectRegex required a sort(), while we can traverse in another order to avoid the sort.

for (let currLength = subject.length; currLength > 0; currLength--) {
  for (
    let startIndex = 0;
    startIndex + currLength <= subject.length;
    startIndex++
  ) {
    let sequence = subject.substr(startIndex, currLength);

Also, it generates duplicated substrings. I haven't checked its impact on Regex.