public
anonymous / benchmark.pl
Created

Simple Mojo::DOM vs HTML::TreeBuilder comparison

  • Download Gist
benchmark.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#!/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% --

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.