Created
January 6, 2009 06:41
-
-
Save brynary/43706 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'open-uri' | |
require 'hpricot' | |
require File.expand_path(File.dirname(__FILE__) + "/lib/dryopteris") | |
require 'benchmark' | |
require "action_view" | |
require "sanitize" | |
class RailsSanitize | |
include ActionView::Helpers::SanitizeHelper | |
extend ActionView::Helpers::SanitizeHelper::ClassMethods | |
end | |
class HTML5libSanitize | |
require 'html5/html5parser' | |
require 'html5/liberalxmlparser' | |
require 'html5/treewalkers' | |
require 'html5/treebuilders' | |
require 'html5/serializer' | |
require 'html5/sanitizer' | |
include HTML5 | |
def sanitize(html) | |
HTMLParser.parse_fragment(html, { | |
:tokenizer => HTMLSanitizer, | |
:encoding => 'utf-8', | |
:tree => TreeBuilders::REXML::TreeBuilder | |
}).to_s | |
end | |
end | |
uri = URI.parse('http://www.google.com/') | |
content = uri.read | |
N = 1000 | |
Benchmark.bm do |x| | |
x.report('Dryopteris') do | |
N.times do | |
Dryopteris.sanitize(content) | |
end | |
end | |
x.report('ActionView') do | |
sanitizer = RailsSanitize.new | |
N.times do | |
sanitizer.sanitize(content) | |
end | |
end | |
x.report('Sanitize') do | |
N.times do | |
Sanitize.clean(content, Sanitize::Config::RELAXED) | |
end | |
end | |
x.report('HTML5lib') do | |
sanitizer = HTML5libSanitize.new | |
N.times do | |
sanitizer.sanitize(content) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user system total real | |
Dryopteris 2.710000 0.060000 2.770000 ( 2.821071) | |
ActionView 8.190000 0.030000 8.220000 ( 8.230527) | |
Sanitize 18.370000 1.030000 19.400000 ( 20.467766) | |
HTML5lib 80.910000 0.450000 81.360000 ( 82.764034) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment