Skip to content

Instantly share code, notes, and snippets.

@jfairbairn
Created March 22, 2011 23:37
Show Gist options
  • 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

Charles Lowell's The Ruby Racer lets you embed a V8 JavaScript engine in your Ruby process, and lets you invoke methods on JavaScript objects from Ruby, and vice versa.

Tim Caswell's haml-js is an implementation of the HAML templating language in JavaScript, that claims millions of renders per second when used directly in V8.

It turns out that even going through The Ruby Racer, rendering a trivial HAML template ("=name") in haml-js is still four times faster than doing it in Ruby HAML. That's nuts!

Here's the time taken for 100k renders:

                       user     system      total        real
haml js from ruby:  1.460000   0.010000   1.470000 (  1.461789)
haml rb:            5.860000   0.130000   5.990000 (  5.978104)

This actually has applications beyond just going a bit faster. If you restrict the objects you push into your templates' scope to JSON-friendly ones, you can have the same templates running on the server and in the browser. This means that, in the browser, you can pass JSON AJAX responses to a HAML snippet and have it run through the same code path as you did when you were rendering the whole page to HTML on the server side, thus removing a common point of duplication in mixed AJAX/HTML webapps.

Epic yay!

@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