clj-fast-html is a Clojure library designed for fast and efficient HTML generation.
You can find the latest version on Clojars:
Here is an example of how to use clj-fast-html to generate an HTML string:
(clj-fast-html.core/to-html-string
[[:html/raw "<!DOCTYPE html>"]
[:html
[:head
[:title "Title"]]
[:body
[:h1#id.class1.class2 "Header"]
[:p {:style {:color "red"}} "text"]
[:p {:class ["class3" "class4"]} "text"]
[:p {:class "class3 class4"} "text"]
[:dialog {:open true} "text"]
[:span {:href (clj-fast-html.core/escape "' onmouseover='alert(1)")}]
[:span (clj-fast-html.core/escape "<p onmouseover='alert(1)'></p>")]
(mapv
(fn [x]
[:span x])
[1 2 3 4])]]])
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1 id="id" class="class1 class2">Header</h1>
<p style="color:red">text</p>
<p class="class3 class4">text</p>
<p class="class3 class4">text</p>
<dialog open>text</dialog>
<span href="' onmouseover='alert(1)"></span>
<span><p onmouseover='alert(1)'></p></span>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</body>
</html>
The performance of clj-fast-html is benchmarked against other popular HTML generation libraries. Below are the benchmark results for generating the Clojure home page
Library | Average (ns) | % |
---|---|---|
clj-fast-html (latest) | 39434.940 ns/op | 0% |
Hiccup (2.0.0-RC3) | 306147.614 ns/op | 676.34% |
Hiccup (2.0.0-RC1) | 666406.325 ns/op | 1589.89% |
Library | Average (μs) | % |
---|---|---|
clj-fast-html (latest) | 176.198 μs/op | 0% |
Hiccup (2.0.0-RC3) | 1332.627 μs/op | 656.32% |
Hiccup (2.0.0-RC1) | 2041.248 μs/op | 1058.5% |
To run the benchmark yourself, use the following command:
make bench