Skip to content

Instantly share code, notes, and snippets.

@kberov
Created November 9, 2012 22:53
Show Gist options
  • Save kberov/4048857 to your computer and use it in GitHub Desktop.
Save kberov/4048857 to your computer and use it in GitHub Desktop.
HTML::TreeBuilder VS Mojo::DOM
#!/usr/bin/env perl
use 5.14.2;
use Benchmark qw(timethese cmpthese);
use HTML::TreeBuilder;
use Mojo::UserAgent;
use Mojo::DOM;
my $url = 'http://www.i-can.eu/index.html';
my $content = Mojo::UserAgent->new->get($url)->res->body;
#PARSING SPEED
my ($root, $dom);
say 'PARSING SPEED';
timethese(
500,
{ 'HTML::TreeBuilder->new_from_content' =>
sub { $root = HTML::TreeBuilder->new_from_content($content); },
'Mojo::DOM->new' => sub { $dom = Mojo::DOM->new($content) },
}
);
#GETTING SOME ELEMENTS
say 'GETTING SOME ELEMENTS';
timethese(
1000,
{ '$root->look_down' => sub {
$root->look_down(_tag => 'h2')->as_trimmed_text
. $root->look_down(class => 'ui-widget-content')->as_HTML();
},
'$dom->at' => sub { $dom->at('h2')->text . $dom->at('.ui-widget-content')->to_xml },
}
);
#OVERALL
say 'OVERALL';
timethese(
1500,
{ '$root' => sub {
$root = HTML::TreeBuilder->new_from_content($content);
$root->look_down(_tag => 'h2')->as_trimmed_text
. $root->look_down(class => 'ui-widget-content')->as_HTML();
},
'$dom' => sub {
$dom = Mojo::DOM->new($content);
$dom->at('h2')->text . $dom->at('.ui-widget-content')->to_xml;
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment