Clj JSON benchmark

{:deps {org.clojure/data.json {:mvn/version "1.0.0"}
        cheshire {:mvn/version "5.10.0"}
        compliment {:mvn/version "0.3.10"}}}
deps.edn
Extensible Data Notation
(defn gen-keyword [] (keyword (gensym)))
(def generate-ds 
  (memoize 
   (fn [x]
     (loop [x x res {}]
       (cond
         (= x 0) res
         (< (rand) 0.5) ; to also get some depth 
         (recur (dec x) (assoc res (gen-keyword) (generate-ds (quot x 2))))
         :else (recur (dec x) (assoc res (gen-keyword) (gen-keyword))))))))
0.0s
Clojure
'user/generate-ds

Generate a larger datastructure.

(def ds (generate-ds 200))
0.1s
Clojure
'user/ds
(require '[cheshire.core :as ches-json])
(require '[clojure.data.json :as clj-json])
1.4s
Clojure
nil

Generating a JSON string. First with cheshire .

(time (ches-json/generate-string ds))
2.4s
Clojure

Then with the standard contrib library.

(time (clj-json/json-str ds))
2.2s
Clojure
(def json-encoded *1)
0.0s
Clojure
'user/json-encoded

Decoding

(time (ches-json/parse-string json-encoded true))
2.9s
Clojure
(time (clj-json/read-str json-encoded :key-fn keyword))
2.6s
Clojure

Write to file

(time (ches-json/with-writer 
       [(clojure.java.io/writer "results/ds.json") {}]
       (ches-json/write ds)))
1.6s
Clojure
Vector(3) [java.io.BufferedWriter, 630748801, "java.io.BufferedWriter@25987681"]
nil
(time (with-open [w (clojure.java.io/writer "results/ds.json")]
        (clj-json/write ds w)))
0.8s
Clojure
nil
nil

Surprisingly, when writing to file the standard clj contrib library is a lot faster.

Read from file

(time (with-open [r (clojure.java.io/reader 
ds.json
)]
        (ches-json/parse-stream r keyword)))
2.2s
Clojure
(time (with-open [r (clojure.java.io/reader 
ds.json
)]
        (clj-json/read r :key-fn keyword)))
2.2s
Clojure
Runtimes (1)