Created
October 4, 2012 23:54
-
-
Save anonymous/3837205 to your computer and use it in GitHub Desktop.
Simple Mojo::DOM vs HTML::TreeBuilder comparison
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 perl | |
use strict; | |
use warnings; | |
use HTML::TreeBuilder; | |
use Mojo::DOM; | |
use Benchmark qw(:hireswallclock cmpthese); | |
use Template; | |
my $tt = Template->new; | |
$tt->process(\<<'END', { row_data => [1..100] }, \my $content) or die $tt->error; | |
<html> | |
<head> | |
<title> Example </title> | |
<style> | |
p{color: red; | |
background-color: #FFFF; | |
} | |
div {...... | |
... | |
} | |
</style> | |
<link rel="stylesheet" src="file.css"> | |
</head> | |
<body> | |
<p> hi I'm a paragraph. </p> | |
<table> | |
[% FOREACH row IN row_data -%] | |
<tr><td>Row [% loop.count %]</td><td>Second column</td></tr> | |
[% END -%] | |
</table> | |
</body> | |
</html> | |
END | |
cmpthese -10, { | |
tree => sub { | |
my $dom = HTML::TreeBuilder->new_from_content($content); | |
$_->delete for $dom->look_down(_tag => 'style'), $dom->look_down(_tag => 'link', rel => 'stylesheet'); | |
# don't want to print it for the benchmark, but include html rendering time anyway | |
my $txt = $dom->as_HTML; # ('<>&', ' '); | |
}, | |
tree_combined => sub { | |
my $dom = HTML::TreeBuilder->new_from_content($content); | |
$_->delete for $dom->look_down(sub { return 1 if $_[0]->tag eq 'style'; return 1 if $_[0]->tag eq 'link' && $_[0]->attr('rel') eq 'stylesheet'; return 0 }); | |
$_->delete for $dom->look_down(_tag => qr/^style|link$/, sub { $_[0]->tag eq 'link' ? $_[0]->attr('rel') eq 'stylesheet' : 1 }); | |
# don't want to print it for the benchmark, but include html rendering time anyway | |
my $txt = $dom->as_HTML; # ('<>&', ' '); | |
}, | |
tree_coderef => sub { | |
my $dom = HTML::TreeBuilder->new_from_content($content); | |
$_->delete for $dom->look_down(sub { return 1 if $_[0]->tag eq 'style'; return 1 if $_[0]->tag eq 'link' && $_[0]->attr('rel') eq 'stylesheet'; return 0 }); | |
# $_->delete for $dom->look_down(_tag => qr/^style|link$/, sub { return 1 if $_[0]->tag eq 'style'; return 1 if $_[0]->tag eq 'link' && $_[0]->attr('rel') eq 'stylesheet'; return 0 }); | |
# don't want to print it for the benchmark, but include html rendering time anyway | |
my $txt = $dom->as_HTML; # ('<>&', ' '); | |
}, | |
mojo => sub { | |
my $dom = Mojo::DOM->new( $content ); | |
$dom->find('style, link[rel=stylesheet]')->pluck('remove'); | |
# don't want to print it for the benchmark, but include html rendering time anyway | |
my $txt = "$dom"; | |
}, | |
}; | |
__END__ | |
Rate tree_combined tree tree_coderef mojo | |
tree_combined 52.4/s -- -2% -2% -24% | |
tree 53.7/s 2% -- -0% -22% | |
tree_coderef 53.8/s 3% 0% -- -22% | |
mojo 68.8/s 31% 28% 28% -- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment