Skip to content

Instantly share code, notes, and snippets.

@timruffles
Created October 15, 2011 16:04
Show Gist options
  • Select an option

  • Save timruffles/1289769 to your computer and use it in GitHub Desktop.

Select an option

Save timruffles/1289769 to your computer and use it in GitHub Desktop.
JS coffee script size comparsion table
require "rubygems"
require "terminal-table/import"
def coffee_length file
non_comment = open(file).read.split("\n").reject {|l| l =~ /^(?:\s+#.*|\s*)$/}
[non_comment.length, character_length(non_comment)]
end
def js_length file
non_comment = open(file).read.split("\n").reject {|l| l =~ /^(?:\s+\/\/.*|\s+\/\*[^\*]*\/\s*|\s*)$/}
[non_comment.length, character_length(non_comment)]
end
def character_length lines
lines.inject(0) {|sum,line| line.gsub(/\s+/,"").length + sum }
end
def to_js file
dir = file.split("/")[3..-2]
file = file.split("/").last.split(".").first
"public/js/plv/#{dir.join("/")}/#{file}.js"
end
puts to_js "app/coffee/plv/models/thing.coffee"
comparison = table ["File","Coffee Lines","Coffee Characters","JS Lines","JS Characters"]
coffee = Dir.glob("app/coffee/plv/**/*.coffee").reject {|file| open(file).read.split("\n").length < 5}
totals = ["Total",0,0,0,0]
coffee.each do |file|
lines, chars = coffee_length file
js_lines, js_chars = js_length to_js file
[lines,chars,js_lines,js_chars].each_with_index do |val,index|
totals[index + 1] += val
end
comparison << [file,lines,chars,"#{"x %.1f" % ((js_lines / lines.to_f))}", "#{"x %.1f" % ((js_chars / chars.to_f))}"]
end
comparison << :separator
comparison << totals
comparison << ["Diff","","","+ #{totals[3] - totals[1]}","+ #{totals[4] - totals[2]}"]
comparison << :separator
comparison << ["Ratios JS/Coffee","","","x %.3f" % (totals[3] / totals[1].to_f),"x %.3f" % (totals[4] / totals[2].to_f)]
puts comparison
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment