Browse Source

perf(readability): small performance improvements

- Don't create a string by concatenation on the heap to then pass it to a
  strings.Builder, pass everything, one by one, to it instead.
- Lower the complexity of shouldRemoveCandidate from quadratic to linear.
jvoisin 1 month ago
parent
commit
28441d1e27
1 changed files with 17 additions and 10 deletions
  1. 17 10
      internal/reader/readability/readability.go

+ 17 - 10
internal/reader/readability/readability.go

@@ -173,13 +173,20 @@ func getArticle(topCandidate *candidate, candidates candidateList) string {
 
 		if append {
 			html, _ := s.Html()
-			output.WriteString("<" + tag + ">" + html + "</" + tag + ">")
+			output.WriteByte('<')
+			output.WriteString(tag)
+			output.WriteByte('>')
+			output.WriteString(html)
+			output.WriteString("</")
+			output.WriteString(tag)
+			output.WriteByte('>')
 		}
 	})
 
 	output.WriteString("</div>")
 	return output.String()
 }
+
 func shouldRemoveCandidate(str string) bool {
 	str = strings.ToLower(str)
 
@@ -190,17 +197,17 @@ func shouldRemoveCandidate(str string) bool {
 		}
 	}
 
+	isFalsePositive := false
+	for _, maybeCandidateToRemove := range maybeCandidateToRemove {
+		if strings.Contains(str, maybeCandidateToRemove) {
+			isFalsePositive = true
+			break
+		}
+	}
+
 	for _, unlikelyCandidateToRemove := range unlikelyCandidateToRemove {
 		if strings.Contains(str, unlikelyCandidateToRemove) {
-			// Do we have a false positive?
-			for _, maybeCandidateToRemove := range maybeCandidateToRemove {
-				if strings.Contains(str, maybeCandidateToRemove) {
-					return false
-				}
-			}
-
-			// Nope, it's a true positive!
-			return true
+			return !isFalsePositive
 		}
 	}
 	return false