Skip to content

Instantly share code, notes, and snippets.

@ZenCocoon
Last active December 17, 2015 15:39
Show Gist options
  • Select an option

  • Save ZenCocoon/5633669 to your computer and use it in GitHub Desktop.

Select an option

Save ZenCocoon/5633669 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'nokogiri'
n = 50000
Benchmark.bm do |x|
x.report { doc = Nokogiri::HTML::DocumentFragment.parse("<p>Foo Bar</p> <p>bar bar bar</p> <p>bla</p>") ; doc.children.map{|node| node.to_s.strip}.compact.join }
x.report { doc = Nokogiri::HTML::DocumentFragment.parse("<p>Foo Bar</p> <p>bar bar bar</p> <p>bla</p>") ; doc.children.each { |node| next_node = node.next_sibling ; next_node.remove if next_node && next_node.text.strip == '' } }
x.report { doc = Nokogiri::HTML("<p>Foo Bar</p> <p>bar bar bar</p> <p>bla</p>") ; doc.search('p').each { |node| next_node = node.next_sibling ; next_node.remove if next_node && next_node.text.strip == '' } }
end
# Results
user system total real
0.000000 0.000000 0.000000 ( 0.000460)
0.000000 0.000000 0.000000 ( 0.000181)
0.000000 0.000000 0.000000 ( 0.000117)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment