From ac9543d9017fbb56e670c0728173432a879d4d13 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 9 Apr 2016 06:32:51 +0100 Subject: Minor performance tweak. Two minor tweaks: 1. Do not use ".indexOf()" three times for each comparison; do it just once. 2. Check the common case (no match) first. These are very minor performance tweaks; but, with global link hints, we pay this cost n times (for n frames). --- content_scripts/link_hints.coffee | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/content_scripts/link_hints.coffee b/content_scripts/link_hints.coffee index 08c5cd9b..6ea10377 100644 --- a/content_scripts/link_hints.coffee +++ b/content_scripts/link_hints.coffee @@ -506,14 +506,15 @@ class FilterHints for searchWord in searchWords linkWordScores = for linkWord, idx in linkWords - if linkWord == searchWord - if idx == 0 then 8 else 6 - else if linkWord.startsWith searchWord - if idx == 0 then 4 else 2 - else if 0 <= linkWord.indexOf searchWord - 1 + position = linkWord.indexOf searchWord + if position < 0 + 0 # No match. + else if position == 0 and searchWord.length == linkWord.length + if idx == 0 then 8 else 6 # Whole-word match. + else if position == 0 + if idx == 0 then 4 else 2 # Match at the start of a word. else - 0 + 1 # 0 < position; other match. Math.max linkWordScores... if 0 in searchWordScores -- cgit v1.2.3