Skip to content

Instantly share code, notes, and snippets.

@mbajur
Last active January 4, 2022 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mbajur/6aa3a4189f4aba15f985672f87ab9f40 to your computer and use it in GitHub Desktop.
Save mbajur/6aa3a4189f4aba15f985672f87ab9f40 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
handlebars  25.512891  0.134474   25.647365 ( 26.928544)
liquid      1.622490   0.009660   1.632150  (  1.656763)
erb         2.215501   0.017284   2.232785  (  2.550151)
gsub        0.050991   0.000783   0.051774  (  0.055585)
@lucasprag
Copy link

lucasprag commented May 7, 2021

If anyone is planing to run this benchmark: The labels are flipped. The label for liquid is actually running handlebars and the label for handlebars is actually running liquid.

@mbajur
Copy link
Author

mbajur commented May 7, 2021

Good catch! I will update that in a bit

Edit: labels and results updated

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