Skip to content

Instantly share code, notes, and snippets.

@iafan
Created May 29, 2017 19:36
Show Gist options
  • Save iafan/e3832ab18ad8bdd9d42d83115967024b to your computer and use it in GitHub Desktop.
Save iafan/e3832ab18ad8bdd9d42d83115967024b to your computer and use it in GitHub Desktop.
Small tool to report the size of Go packages in the GopherJS-compiled file
#!/usr/bin/perl
# This script can be used to see the size of each Go package
# in the JavaScript file complied by GopherJS.
#
# Run:
#
# ./analyze-gopherjs client.js
#
# to analyze client.js file, or:
#
# ./analyze-gopherjs client.js | grep http
#
# to just show the lines containing 'http' pacakge name.
use strict;
my $size = {};
my $total = 0;
my $file = $ARGV[0];
if ($file eq '') {
print "Usage: $0 <path/to/compiled.js>\n";
exit(1);
}
open(IN, $file) or die $!;
while (my $line = <IN>) {
if ($line =~ m/^\$packages\["(.*?)"\]=/) {
$size->{$1} = length($line);
$total += $size->{$1};
}
}
close IN;
foreach my $pkg (sort { $size->{$b} <=> $size->{$a} } keys %$size) {
my $percent = $size->{$pkg} / $total * 100;
$percent = int($percent * 100) / 100;
print $pkg, " => ", $size->{$pkg}, " bytes ($percent%)\n";
}
@arlimus
Copy link

arlimus commented Jul 22, 2018

Thank you, this was really helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment