Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
(ns benchmark
(:require [hiccup.core :as hiccup]
[clj-html.core :as clj-html]))
(defn clj-html-benchmark []
(let [text "Some text"]
(clj-html/html
[:html
[:head
[:title "Literal String"]]
[:body
[:div.example text]
[:ul.times-table
(for [n (range 1 13)]
[:li n " * 9 = " (* n 9)])]]])))
(defn hiccup-benchmark []
(let [text "Some text"]
(hiccup/html
[:html
[:head
[:title "Literal String"]]
[:body
[:div.example text]
[:ul.times-table
(for [n (range 1 13)]
[:li n " * 9 = " (* n 9)])]]])))
(defn hint-hiccup-benchmark []
(let [text "Some text"]
(hiccup/html
[:html
[:head
[:title "Literal String"]]
[:body
[:div.example #^String text]
[:ul.times-table
(for [n (range 1 13)]
[:li #^Number n " * 9 = " (* #^Number n 9)])]]])))
(defn str-benchmark []
(let [text "Some text"]
(str "<html><head><title>Literal String</title</head>"
"<body><div class=\"example\">" text "</div>"
"<ul class=\"times-table\">"
(apply str
(for [n (range 1 13)]
(str "<li>" n " * 9 = " (* n 9) "</li>")))
"</ul></body></html>")))
(defn run-benchmark [f]
(dotimes [_ 3]
(time (dotimes [_ 100000] (f)))))
(println "clj-html")
(run-benchmark clj-html-benchmark)
(println "hiccup")
(run-benchmark hiccup-benchmark)
(println "hiccup (type-hint)")
(run-benchmark hint-hiccup-benchmark)
(println "str")
(run-benchmark str-benchmark)
clj-html
"Elapsed time: 5216.735225 msecs"
"Elapsed time: 4956.232717 msecs"
"Elapsed time: 4935.857954 msecs"
hiccup
"Elapsed time: 1523.607288 msecs"
"Elapsed time: 1455.099768 msecs"
"Elapsed time: 1461.435383 msecs"
hiccup (type-hint)
"Elapsed time: 1303.880541 msecs"
"Elapsed time: 1252.724691 msecs"
"Elapsed time: 1251.617778 msecs"
str
"Elapsed time: 1288.655252 msecs"
"Elapsed time: 1216.06278 msecs"
"Elapsed time: 1217.776707 msecs"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment