⚡ Optimize Google Scholar Citations Regex Definition (#3449)
💡 **What:** Moved the regex definition `/Cited by (\d+[,\d]*)/` from the method scope to a class-level constant `CITED_BY_REGEX`. 🎯 **Why:** To improve code cleanliness and avoid potential re-definition of the regex object in every method call (or loop), adhering to best practices. 📊 **Measured Improvement:** * **Baseline:** The regex was defined as a literal inside the `render` method, which is called for each tag usage. * **Optimization:** The regex is now defined once as a constant. * **Note:** Performance benchmarks were not possible in the current environment due to missing Ruby runtime. However, this is a standard Ruby optimization that improves maintainability and theoretically avoids object allocation overhead in older Ruby versions or complex scenarios. Modern Ruby optimizes literals well, but the constant approach is cleaner and DRYer. --- *PR created automatically by Jules for task [10688912524063334698](https://jules.google.com/task/10688912524063334698) started by @alshedivat* Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
parent
514533c50a
commit
2e308ed606
@ -9,6 +9,7 @@ end
|
||||
module Jekyll
|
||||
class GoogleScholarCitationsTag < Liquid::Tag
|
||||
Citations = { }
|
||||
CITED_BY_REGEX = /Cited by (\d+[,\d]*)/
|
||||
|
||||
def initialize(tag_name, params, tokens)
|
||||
super
|
||||
@ -51,7 +52,7 @@ module Jekyll
|
||||
|
||||
if !description_meta.empty?
|
||||
cited_by_text = description_meta[0]['content']
|
||||
matches = cited_by_text.match(/Cited by (\d+[,\d]*)/)
|
||||
matches = cited_by_text.match(CITED_BY_REGEX)
|
||||
|
||||
if matches
|
||||
citation_count = matches[1].sub(",", "").to_i
|
||||
@ -59,7 +60,7 @@ module Jekyll
|
||||
|
||||
elsif !og_description_meta.empty?
|
||||
cited_by_text = og_description_meta[0]['content']
|
||||
matches = cited_by_text.match(/Cited by (\d+[,\d]*)/)
|
||||
matches = cited_by_text.match(CITED_BY_REGEX)
|
||||
|
||||
if matches
|
||||
citation_count = matches[1].sub(",", "").to_i
|
||||
|
||||
Loading…
Reference in New Issue
Block a user