Skip to content

Commit 6c26a89

Browse files
authored
[ruby] Use Rack::Utils.escape_html for escaping (#10032)
Rack::Utils.escape_html is faster than CGI.escape_html: ```ruby require 'benchmark/ips' require 'cgi' require 'rack' Benchmark.ips do |x| x.config(warmup: 2, time: 5) message = 'Additional fortune added at request time.' x.report("CGI") do CGI.escape_html(message) end x.report("Rack") do Rack::Utils.escape_html(message) end x.compare! end ``` ``` ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [arm64-darwin24] Warming up -------------------------------------- CGI 1.016M i/100ms Rack 1.586M i/100ms Calculating ------------------------------------- CGI 10.821M (± 0.3%) i/s (92.42 ns/i) - 54.837M in 5.067830s Rack 15.861M (± 0.3%) i/s (63.05 ns/i) - 80.886M in 5.099606s Comparison: Rack: 15861352.0 i/s CGI: 10820753.9 i/s - 1.47x slower ```
1 parent 019490c commit 6c26a89

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

frameworks/Ruby/rage-sequel/app/views/fortunes.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<table>
66
<tr><th>id</th><th>message</th></tr>
77
<% records.each do |record| %>
8-
<tr><td><%= record.id %></td><td><%= CGI.escape_html(record.message) %></td></tr>
8+
<tr><td><%= record.id %></td><td><%= Rack::Utils.escape_html(record.message) %></td></tr>
99
<% end %>
1010
</table>
1111
</body>

frameworks/Ruby/rage/app/views/fortunes.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<table>
66
<tr><th>id</th><th>message</th></tr>
77
<% records.each do |record| %>
8-
<tr><td><%= record[:id] %></td><td><%= CGI.escape_html(record[:message]) %></td></tr>
8+
<tr><td><%= record[:id] %></td><td><%= Rack::Utils.escape_html(record[:message]) %></td></tr>
99
<% end %>
1010
</table>
1111
</body>

0 commit comments

Comments
 (0)