Skip to content

Commit

Permalink
cache regex instantiations
Browse files Browse the repository at this point in the history
  • Loading branch information
JaneJeon committed Sep 15, 2021
1 parent 5fccc9f commit d5455eb
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions utils/strip-trackers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import logger from './logger.js'
const providers = load()
const debug = logger('utils/strip-trackers.js')

const cachedRegex = mem(pattern => new RegExp(pattern))

function clearUrl(url) {
debug('Stripping trackers for %s', url)

Expand All @@ -16,12 +18,10 @@ function clearUrl(url) {

// Shamelessly adopted from https://gitlab.com/CrunchBangDev/cbd-cogs/-/blob/master/Scrub/scrub.py
// TODO: benchmark regex search
// TODO: cache new RegExp() calls...?
// For now, this is a version written w/o any performance shit in mind

providers.forEach(provider => {
// Check provider urlPattern against provided URI
if (!new RegExp(provider.urlPattern).test(url)) return
if (!cachedRegex(provider.urlPattern).test(url)) return
debug('Matched a provider %s', provider.urlPattern)

// completeProvider is a boolean that determines if every url that
Expand All @@ -31,15 +31,15 @@ function clearUrl(url) {

// If any exceptions are matched, this provider is skipped
for (const exception of provider.exceptions || []) {
if (new RegExp(exception).test(url)) {
if (cachedRegex(exception).test(url)) {
debug('Matched an exception %s. Skipping...', exception)
return
}
}

// the redirections from this handles cases like youtube redirects where you literally CAN'T be redirected by an HTTP call because youtube is a piece of fucking shit
for (const redir of provider.redirections || []) {
const regex = new RegExp(redir)
const regex = cachedRegex(redir)
const match = regex.exec(url)
if (match && match.length > 1) {
url = decodeURIComponent(match[1])
Expand All @@ -52,7 +52,7 @@ function clearUrl(url) {

// Check regular rules and referral marketing rules
for (const rule of provider.rules || []) {
const regex = new RegExp(rule)
const regex = cachedRegex(rule)
for (const param of parsedUrl.searchParams.keys()) {
if (regex.test(param)) {
parsedUrl.searchParams.delete(param)
Expand All @@ -61,7 +61,7 @@ function clearUrl(url) {
}
}
for (const rule of provider.referralMarketing || []) {
const regex = new RegExp(rule)
const regex = cachedRegex(rule)
for (const param of parsedUrl.searchParams.keys()) {
if (regex.test(param)) {
parsedUrl.searchParams.delete(param)
Expand All @@ -78,7 +78,7 @@ function clearUrl(url) {

// Strip raw fragments with rawRules
for (const rule of provider.rawRules || []) {
const regex = new RegExp(rule)
const regex = cachedRegex(rule)
url = url.replace(regex, '')
debug('Stripped raw fragment %s to get %s', rule, url)
}
Expand Down

0 comments on commit d5455eb

Please sign in to comment.