Skip to content

Instantly share code, notes, and snippets.

@lucasprag
Forked from mbajur/Gemfile
Last active May 7, 2021 14:24
Show Gist options
  • Save lucasprag/ca9868fb8e3438e915e039197bbd8f54 to your computer and use it in GitHub Desktop.
Save lucasprag/ca9868fb8e3438e915e039197bbd8f54 to your computer and use it in GitHub Desktop.
Liquid, Handlebars, Erb and gsub benchmarks
require 'rubygems'
require 'benchmark'
require 'ruby-handlebars'
require 'cgi'
require 'liquid'
require 'erb'
input = ("a".."z").map { |letter| [letter, letter] }.to_h
n = 50_000
hbs = Handlebars::Handlebars.new
Benchmark.bm do |benchmark|
benchmark.report("handlebars") do
n.times do
hbs.compile("Hello {{name}}").call({ name: 'world' })
end
end
benchmark.report("liquid") do
n.times do
Liquid::Template.parse("Hello {{name}}").render({ name: 'world' })
end
end
benchmark.report("erb") do
n.times do
ERB.new("Hello <%= name %>").result_with_hash({ name: 'world' })
end
end
benchmark.report("gsub") do
n.times do
"Hello {{name}}".gsub('{{name}}', 'world')
end
end
end
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem 'liquid'
gem 'ruby-handlebars'
            user       system     total     real
liquid      26.567868  0.136828   26.704696 ( 27.704823)
handlebars  1.579311   0.005023   1.584334  (  1.613605)
erb         2.157431   0.007026   2.164457  (  2.187186)
gsub        0.047701   0.000408   0.048109  (  0.055053)
@elyalvarado
Copy link

Hey @lucasprag, I believe the name of the reports are swapped: you're reporting liquid for the handlebars results and viceversa

@sivakumar-kailasam
Copy link

sivakumar-kailasam commented May 5, 2021

These are the stats on my machine with the labels corrected,

library user system total real
handlebars 19.023716 0.050215 19.073931 ( 19.264012)
liquid 1.631314 0.006280 1.637594 ( 1.665485)
erb 1.883368 0.006673 1.890041 ( 1.921382)
gsub 0.044841 0.000206 0.045047 ( 0.049943)

my config:
Screen Shot 2021-05-05 at 7 51 04 PM

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