|
| 1 | +use lib './lib'; |
| 2 | +use strict; |
| 3 | +use warnings; |
| 4 | +use JavaScript::Duktape; |
| 5 | +use Data::Dumper; |
| 6 | +use Benchmark qw(:all :hireswallclock) ; |
| 7 | + |
| 8 | +my $js = JavaScript::Duktape->new(); |
| 9 | +my $duk = $js->duk; |
| 10 | + |
| 11 | +my $time = 5; |
| 12 | +my $count = 25000; |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +{ ##settung global functions |
| 17 | + |
| 18 | + my $t = timeit($count, sub{ |
| 19 | + $js->set('test', sub{}); |
| 20 | + }); |
| 21 | + print_results("set global functions", $t); |
| 22 | +} |
| 23 | + |
| 24 | +{ ##getting javascript object |
| 25 | + |
| 26 | + my $t = timeit($count, sub{ |
| 27 | + $duk->eval_string(qq~ |
| 28 | + function javascriptObject1 (){} |
| 29 | + javascriptObject1; |
| 30 | + ~); |
| 31 | + |
| 32 | + my $obj = $duk->to_perl_object(-1); |
| 33 | + $duk->pop(); |
| 34 | + }); |
| 35 | + print_results("getting objects", $t); |
| 36 | +} |
| 37 | + |
| 38 | +{ ##object operations |
| 39 | + $duk->eval_string(qq~ |
| 40 | + function javascriptObject (){ |
| 41 | + this.callme = function(n){ |
| 42 | + this.test = n; |
| 43 | + } |
| 44 | + } |
| 45 | + javascriptObject; |
| 46 | + ~); |
| 47 | + my $obj = $duk->to_perl_object(-1); |
| 48 | + $duk->pop(); |
| 49 | + |
| 50 | + { #calling new on objects |
| 51 | + my $t = timeit($count, sub{ |
| 52 | + my $o = $obj->new(); |
| 53 | + }); |
| 54 | + print_results("calling new", $t); |
| 55 | + } |
| 56 | + |
| 57 | + { #calling function from objects |
| 58 | + my $o = $obj->new(); |
| 59 | + my $i = 0; |
| 60 | + my $t = timeit($count, sub{ |
| 61 | + $o->callme($i++); |
| 62 | + # print $o->test, "\n"; |
| 63 | + }); |
| 64 | + print_results("calling function", $t); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +sub print_results { |
| 69 | + my $name = shift; |
| 70 | + my $t = shift; |
| 71 | + |
| 72 | + my @result = split('@', timestr($t)); |
| 73 | + |
| 74 | + print "===================================\n"; |
| 75 | + print "$count $name\n"; |
| 76 | + print "===================================\n"; |
| 77 | + print " " . $result[0], "\n"; |
| 78 | + print $result[1], "\n"; |
| 79 | + print "\n\n"; |
| 80 | +} |
0 commit comments