From 6e667e078c6495a44cff10b8e548a74888f18f66 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Thu, 24 Feb 2022 23:41:03 +0100 Subject: [PATCH] :tada: Add cljs benchmark code under dev directory --- frontend/dev/cljs/user.cljs | 65 +++++++++++++++++++++++++++++++++++-- frontend/shadow-cljs.edn | 17 ++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/frontend/dev/cljs/user.cljs b/frontend/dev/cljs/user.cljs index 3a927046b..7fb44ff0e 100644 --- a/frontend/dev/cljs/user.cljs +++ b/frontend/dev/cljs/user.cljs @@ -1,5 +1,64 @@ -(ns cljs.user) +(ns cljs.user + (:require + [app.common.data :as d] + [app.common.data.macros :as dm] + [app.common.perf :as perf] + [cuerdas.core :as str])) -(defn hello +(def data {:a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :k 8 :l 9 :m {:a {:b {:c {:d {:e {:f 1}}}}}}}) + +(defn bench-get-in + [& args] + (let [iterations 1000000 + keys [:m :a :b :c :d :e :f]] + + (perf/benchmark :f #(get-in data keys nil) + :max-iterations iterations + :name "get-in-initialized") + + (perf/benchmark :f #(get-in data [:m :a :b :c :d :e :f] nil) + :max-iterations iterations + :name "get-in") + + (perf/benchmark :f #(dm/get-in data [:m :a :b :c :d :e :f] nil) + :max-iterations iterations + :name "dm/get-in"))) + +(defn bench-select-keys + [& args] + (let [iterations 1000000 + keys [:k :a :m :d :f :g :k]] + (perf/benchmark :f #(select-keys data keys) + :max-iterations iterations + :name "select-keys-initialized") + + (perf/benchmark :f #(select-keys data [:k :a :m :d :f :g :k]) + :max-iterations iterations + :name "select-keys") + + (perf/benchmark :f #(dm/select-keys data [:k :a :m :d :f :g :k]) + :max-iterations iterations + :name "dm/select-keys"))) + +(defn bench-string-concat [] - (js/console.log "hello")) + (perf/benchmark :f #(str "foo" 2 "bar" 3 "baz" 4 "kkk" 5) + :max-iterations 500000 + :name "clojure.core/str") + + (perf/benchmark :f #(dm/str "foo" 2 "bar" 3 "baz" 4 "kkk" 5) + :max-iterations 500000 + :name "app.commons.data.macros/str") + (let [items ["foo" 2 "bar" 3 "baz" 4 "kkk" 5]] + (perf/benchmark :f #(str/join "" items) + :max-iterations 500000 + :name "cuerdas.core/join"))) + +(defn main + [& [name]] + (case name + "str" (bench-string-concat) + "select-keys" (bench-select-keys) + "get-in" (bench-get-in) + (println "available: str select-keys get-in"))) + diff --git a/frontend/shadow-cljs.edn b/frontend/shadow-cljs.edn index 4bab9ad72..f5fd67d0f 100644 --- a/frontend/shadow-cljs.edn +++ b/frontend/shadow-cljs.edn @@ -59,6 +59,23 @@ :anon-fn-naming-policy :off :source-map-detail-level :all}}} + :bench + {:target :node-script + :output-to "target/bench.js" + :output-dir "target/bench/" + :main cljs.user/main + + :compiler-options + {:output-feature-set :es8 + :output-wrapper false + :warnings {:fn-deprecated false}} + + :release + {:compiler-options + {:fn-invoke-direct true + :elide-asserts true + :anon-fn-naming-policy :off}}} + :test {:target :node-test :output-to "target/tests.js"