diff --git a/CHANGELOG b/CHANGELOG
index 5671d8b1d81ed637b712993510c93b31812e5766..3044ebbd7e4bcfd3f401be6b1a7252e60fa42912 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 Please view this file on the master branch, on stable branches it's out of date.
 
 v 8.2.0 (unreleased)
+  - Improved performance of replacing references in comments
   - Fix duplicate repositories in GitHub import page (Stan Hu)
   - Show last project commit to default branch on project home page
   - Highlight comment based on anchor in URL
diff --git a/lib/gitlab/markdown/reference_filter.rb b/lib/gitlab/markdown/reference_filter.rb
index adaca78ba27282434cedfe5de7d227ebc4a1e18e..a4c560f578cae024b180fb3d05c0f0dd6e85f215 100644
--- a/lib/gitlab/markdown/reference_filter.rb
+++ b/lib/gitlab/markdown/reference_filter.rb
@@ -15,7 +15,7 @@ module Gitlab
       LazyReference = Struct.new(:klass, :ids) do
         def self.load(refs)
           lazy_references, values = refs.partition { |ref| ref.is_a?(self) }
-          
+
           lazy_values = lazy_references.group_by(&:klass).flat_map do |klass, refs|
             ids = refs.flat_map(&:ids)
             klass.where(id: ids)
@@ -107,10 +107,10 @@ module Gitlab
         return doc if project.nil?
 
         search_text_nodes(doc).each do |node|
-          content = node.to_html
-
-          next unless content.match(pattern)
           next if ignored_ancestry?(node)
+          next unless node.text =~ pattern
+
+          content = node.to_html
 
           html = yield content