Skip to content

Instantly share code, notes, and snippets.

@rosenfeld
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosenfeld/9013128 to your computer and use it in GitHub Desktop.
Save rosenfeld/9013128 to your computer and use it in GitHub Desktop.
Concatenating first vs concatenating last performance comparison
environment = Rails.application.assets
asset = nil
environment.send :expire_index!
FileUtils.rm_rf environment.cache.instance_variable_get :@root
puts Benchmark.measure{File.write '/tmp/application-orig.js', (asset = environment['application']).source}.total
puts(Benchmark.measure do
cs_source = []
js_source = []
i = -1
asset.to_a.each do |d|
if File.extname(d.pathname.to_s) == '.coffee'
cs_source.push "do ->\n", d.pathname.read.gsub(/^(.)/, ' \\1')
else
cs_source.push "`class({{#{i += 1}}})`;"
js_source.push d.body
end
end
s=CoffeeScript.compile(cs_source.join)
File.write '/tmp/application.js', js_source.each.with_index.inject(s){|a, (v, i)| a.sub("class({{#{i}}});"){v} }
end.total)
EXECJS_RUNTIME = Node:
9.9
1.7
EXECJS_RUNTIME = RubyRacer:
2.3
2.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment