# \[ANN\] FasterHTMLEscape, an h replacement

**URL:** <https://www.ruby-forum.com/t/ann-fasterhtmlescape-an-h-replacement/79061>\
**Category:** Rails\
**Created:** [December 11, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/ann-fasterhtmlescape-an-h-replacement/79061 "2006-12-11T22:56:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nerdmaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c68b51/32.png) [@nerdmaster](https://www.ruby-forum.com/u/nerdmaster)\
**Post date:** [December 11, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/ann-fasterhtmlescape-an-h-replacement/79061/1 "2006-12-11T22:56:15Z")

</div>

h is a function commonly used in .rhtml templates to escape HTML data  
(e.g. “\<%=h html %\>”). It is defined in ERB::Util#html\_escape (h is  
an alias). It’s implementation is relatively unoptimized, requiring a  
separate gsub for each of \<, \>, &, and ". Ruby’s implementation of  
String#gsub (str\_gsub, written in C), converts all string arguments to  
regular expressions before scanning the input, further slowing things  
down.

I have rewritten html\_escape in C, speeding it up by a factor of 4-20  
depending on the input. Input with no HTML characters is about 4  
times faster, input of exclusively HTML characters is about 20 times  
faster.

Attached is a tarball, which includes:

extconf.rb - for creating the makefile to compile the extension  
faster\_html\_escape.c - The C version of html\_escape (which is created  
inside the FasterHTMLEscape module)  
speed\_up\_html\_escape.rb - This replaces the ERB::Util version of  
html\_escape and h, as well as CGI.escapeHTML, with the C version  
test\_faster\_html\_escape.rb - Small test script for checking the  
validity of the C function as well as benchmarking it  
test\_faster\_html\_escape.sh - Uses test\_faster\_html\_escape.rb to check  
and benchmark the C function with various inputs

To install (assuming Unix conventions):

tar zxf faster\_html\_escape.tar.gz  
cd faster\_html\_escape  
ruby extconf.rb  
make  
./test\_faster\_html\_escape.sh  
sudo make install

To use:

# For example, in environment.rb

require ‘speed\_up\_html\_escape’

If there is interest in this, I can look into packaging it as a gem.

Please try it out and give me feedback.

Thanks,  
Jeremy

---

<div class="post-metadata">

**Author:** ![nerdmaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c68b51/32.png) [@nerdmaster](https://www.ruby-forum.com/u/nerdmaster)\
**Post date:** [December 12, 2006, 5:50pm UTC](https://www.ruby-forum.com/t/ann-fasterhtmlescape-an-h-replacement/79061/2 "2006-12-12T17:50:50Z")

</div>

On 12/11/06, Jeremy E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> depending on the input. Input with no HTML characters is about 4  
> times faster, input of exclusively HTML characters is about 20 times  
> faster.
> 
> Attached is a tarball, which includes:

Great idea Jeremy! Considering that this is used so often in rendering  
html,  
this is a simple way to improve rendering performance especially for  
files  
that have large amounts of html\_escaped content.

Have you considered creating a gem for this?

Ideally I’d like to see this replace the built-in ruby html\_escape once  
adequate testing and analysis has been done on the module, but in the  
meantime it would be great to have it as a gem to lower the barrier of  
entry  
for users (especially windoze users which often don’t have compilers).

Blessings,

Jeff

---

<div class="post-metadata">

**Author:** ![nerdmaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c68b51/32.png) [@nerdmaster](https://www.ruby-forum.com/u/nerdmaster)\
**Post date:** [December 12, 2006, 6:47pm UTC](https://www.ruby-forum.com/t/ann-fasterhtmlescape-an-h-replacement/79061/3 "2006-12-12T18:47:06Z")

</div>

Anything that can reliably improve speed should be greeted with much  
enthusiasm. So, I definitely think there will be interest in this and  
the  
gem idea is a good one.

thanks,  
andy

---

<div class="post-metadata">

**Author:** ![nerdmaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c68b51/32.png) [@nerdmaster](https://www.ruby-forum.com/u/nerdmaster)\
**Post date:** [December 12, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/ann-fasterhtmlescape-an-h-replacement/79061/4 "2006-12-12T23:32:58Z")

</div>

On 12/11/06, Jeremy E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> times faster, input of exclusively HTML characters is about 20 times  
> validity of the C function as well as benchmarking it  
> sudo make install
> 
> To use:
> 
> # For example, in environment.rb
> 
> require ‘speed\_up\_html\_escape’
> 
> If there is interest in this, I can look into packaging it as a gem.
> 
> Please try it out and give me feedback.

I got enough requests for this that I’ve packaged it as a gem and  
applied for a RubyForge project. I’ll post again after the project  
has been approved and the gem is available via gem install. I don’t  
anticipate having a Win32 version, but if you running Rails on Win32,  
you probably don’t care too much about performance.

Thanks,  
Jeremy

---

<div class="post-metadata">

**Author:** ![nerdmaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c68b51/32.png) [@nerdmaster](https://www.ruby-forum.com/u/nerdmaster)\
**Post date:** [December 12, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/ann-fasterhtmlescape-an-h-replacement/79061/5 "2006-12-12T22:20:48Z")

</div>

On Dec 11, 2006, at 1:53 PM, Jeremy E. wrote:

> # For example, in environment.rb
> 
> require ‘speed\_up\_html\_escape’
> 
> If there is interest in this, I can look into packaging it as a gem.
> 
> Please try it out and give me feedback.

Hey Jeremy-

```
This is definitely cool. I would definitely use this as a gem if you

```

make it into one. If you don’t I might vendor it in merb 😉

Thanks-

– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![nerdmaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c68b51/32.png) [@nerdmaster](https://www.ruby-forum.com/u/nerdmaster)\
**Post date:** [December 13, 2006, 1:29am UTC](https://www.ruby-forum.com/t/ann-fasterhtmlescape-an-h-replacement/79061/6 "2006-12-13T01:29:17Z")

</div>

> I got enough requests for this that I’ve packaged it as a gem and  
> applied for a RubyForge project. I’ll post again after the project  
> has been approved and the gem is available via gem install.  
> Very nice  
> I don’t  
> anticipate having a Win32 version, but if you running Rails on Win32,  
> you probably don’t care too much about performance.

lol

Thanks,

matthi

---

<div class="post-metadata">

**Author:** ![nerdmaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c68b51/32.png) [@nerdmaster](https://www.ruby-forum.com/u/nerdmaster)\
**Post date:** [December 13, 2006, 9:01pm UTC](https://www.ruby-forum.com/t/ann-fasterhtmlescape-an-h-replacement/79061/7 "2006-12-13T21:01:30Z")

</div>

On 12/12/06, Jeremy E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > depending on the input. Input with no HTML characters is about 4  
> > times faster, input of exclusively HTML characters is about 20 times  
> > faster.
> > 
> > To use:
> > 
> > # For example, in environment.rb
> > 
> > require ‘speed\_up\_html\_escape’
> 
> I got enough requests for this that I’ve packaged it as a gem and  
> applied for a RubyForge project.

The gem is now available via “gem install faster\_html\_escape”. After  
installing it, add “require ‘speed\_up\_html\_escape’” to environment.rb  
to speed your h up.

If you want to use it outside of Rails:

require ‘rubygems’  
require\_gem ‘faster\_html\_escape’  
require ‘speed\_up\_html\_escape’

Please direct any questions/comments to the RubyForge site:  
[http://rubyforge.org/projects/fasterh/](http://rubyforge.org/projects/fasterh/).

Thanks,  
Jeremy
