Skip to content

Instantly share code, notes, and snippets.

@jfairbairn
Created March 22, 2011 23:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jfairbairn/882338 to your computer and use it in GitHub Desktop.
Save jfairbairn/882338 to your computer and use it in GitHub Desktop.
Using haml-js in Ruby. See comments for what/why. You'll need to get haml.js and copy it to your clone directory to make this work.
#!/usr/bin/env ruby
require 'benchmark'
require 'rubygems'
gem 'therubyracer'
gem 'haml'
gem 'tilt'
require 'v8'
require 'haml'
require 'tilt'
class HamlJs
def initialize
@cxt = V8::Context.new
@cxt.eval(File.read('haml.js'))
end
def template(code)
@cxt['code'] = code
@cxt.eval('Haml(code)')
end
end
hj=HamlJs.new.template(File.read('foo.haml'))
hr=Haml::Engine.new(File.read('foo.haml'), :ugly=>true)
ht=Tilt.new('foo.haml', :ugly=>true)
locals = {'name'=>'hello'}
Benchmark.bm(18) do |bm|
bm.report('haml js from ruby:') { 100000.times { hj.call(locals) }}
bm.report('haml rb:') { 100000.times { hr.render(self, locals) }}
bm.report('tilt/haml rb:') { 100000.times { ht.render(self, locals) }}
end
@jfairbairn
Copy link
Author

With Tilt benchmark (Tilt 1.2.2):

                        user     system      total        real
haml js from ruby:  1.400000   0.000000   1.400000 (  1.399311)
haml rb:            5.590000   0.100000   5.690000 (  5.683828)
tilt/haml rb:       1.390000   0.010000   1.400000 (  1.403251)

@creationix
Copy link

Nice!

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