Skip to content

Instantly share code, notes, and snippets.

@brenogazzola
Last active October 2, 2021 14:57
Show Gist options
  • Save brenogazzola/6af4cb65f791bf295aaad6803570359e to your computer and use it in GitHub Desktop.
Save brenogazzola/6af4cb65f791bf295aaad6803570359e to your computer and use it in GitHub Desktop.
Benching fastest way to grab only the important part of the urls
require 'benchmark/ips'
content = %(
.prop 1 { background: url(file.jpg); }
.prop 2 { background: url( file.jpg ); }
.prop 3 { background: url("file.jpg"); }
.prop 4 { background: url('file.jpg'); }
.prop 5 { background: url('/file.jpg'); }
.prop 6 { background: url('./file.jpg'); }
.prop 7 { background: url('./images/file.jpg'); }
.prop 8 { background: url('../file.jpg'); }
.prop 9 { background: url('../../file.jpg'); }
.prop 10 { background: url('../sibling/file.jpg'); }
.prop 11 { mask-image: image(url(file.jpg), skyblue, linear-gradient(rgba(0, 0, 0, 1.0), transparent)); }
.prop 12 { content: url(file.svg) url(file.svg); }
.prop 13 { mask-image: url("file.svg#mask1"); }
.prop 14 { background: url('https://rubyonrails.org/images/rails-logo.svg'); }
.prop 15 { background: url(data:image/png;base64,iRxVB0…); }
.prop 16 { background: url(#IDofSVGpath); }
)
Benchmark.ips do |x|
x.config(time: 30, warmup: 2)
x.report('baseline') do
content.gsub(/url\(.*?\)/) do |match|
match
end
end
x.report('gsub_wildcard') do
content.gsub(/url\(["']?\s*(.*?)["']?\s*\)/) do
"url('#{$1}')"
end
end
x.report('gsub_except') do
content.gsub(/url\(\s*["']?([^"')]+)\s*["']?\)/) do
"url('#{$1}')"
end
end
x.report('delete') do
content.gsub(/url\((.*?)\)/) do |match|
"url('#{match.delete(" \"'")}')"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment