title: Nokogiri::XML::Builder turns out slower than Builder for generating XML in Rails?
body: <p>We have been using Rails' (2.3.3) Builder to generate XML Markup for our APIs for a while, when we thought that Nokogiri's Builder would generate XML faster (as many have pointed out).</p>
  
  <p>The problem is, my benchmarks showed Nokogiri::Builder slower than regular Builder:</p>
  
  <pre><code>Nokogiri::Builder: 3.1314s
  Builder:           2.1592s
  </code></pre>
  
  <p>for rendering exactly the same request 10 times. I've repeated similar tests in different environments multiple times so I am sure that Nokogiri is running slower.</p>
  
  <p>Our application has lots of .xml.builder views that use many .xml.builder partials:</p>
  
  <pre><code>xml.instruct!
  xml.feed do
    xml.stuff do
      xml.more_stuff(...)
    end
    xml.location do
      xml &lt;&lt; render(:partial =&gt; shared/location, :object =&gt; mylocation)
    end
  end
  </code></pre>
  
  <p>The above utilizes the shared/_location.xml.builder. Our application heavily uses such XML partials to organize code.</p>
  
  <p>I changed to Nokogiri following "<a href="http://matt.aimonetti.net/posts/2010/02/22/speed-up-your-rails-xml-responses/" rel="nofollow">Speed Up Your Rails XML Responses</a>" (see benchmark app there).</p>
  
  <p>My Template handler looks like this:</p>
  
  <pre><code>class NokogiriBuilder &lt; ActionView::TemplateHandler
    include ActionView::TemplateHandlers::Compilable
  
    def compile template
      "_set_controller_content_type(Mime::XML);" +
        "xml = ::Nokogiri::XML::Builder.new(:encoding =&gt; 'UTF-8') { |xml| " +
        template.source +
        '}.to_xml; self.output_buffer = xml;'
    end 
  end
  </code></pre>
  
  <p>When I tried to run my code through rubyprof, results show that create_element takes up a long amount of time!</p>
  
  <pre><code> %self     total     self     wait    child    calls  name
   23.47      1.15     1.15     0.00     0.00      731  Mysql#query
    9.59      0.78     0.47     0.00     0.31     1437  Nokogiri::XML::Document#create_element
    7.96      3.61     0.39     0.00     3.61      867  &lt;Module::Benchmark&gt;#realtime
    5.51      0.27     0.27     0.00     0.00     1167  Nokogiri::XML::Node#encode_special_chars
    5.51      0.28     0.27     0.00     0.01     1370  Module#define_method
    5.10      0.26     0.25     0.00     0.01      346  Array#select
    3.88      0.19     0.19     0.00     0.00     5236  Module#constants
    3.27      0.90     0.16     0.00     0.83     1820  Array#collect
    2.45      0.13     0.12     0.00     0.01     1712  Rack::Lint::Assertion#assert
    2.24      1.09     0.11     0.00     1.09      554  ActiveSupport::CoreExtensions::String::Inflections#constantize
    2.04      0.10     0.10     0.00     0.00       23  Nokogiri::XML::ParseOptions#initialize
    2.04      0.10     0.10     0.00     0.00      726  &lt;Class::ActiveSupport::Callbacks::CallbackChain&gt;#extract_options
  </code></pre>
  
  <p>I'm really wondering what's so special in my case that Nokogiri would turn out slower, though it has proved to be many times faster for many people! </p>
  
  <p>No Tender Lovemaking? :-(</p>