Skip to content

Instantly share code, notes, and snippets.

@minikomi
Created May 26, 2017
Embed
What would you like to do?
(ns objtest.core
(:require [goog.object :as gobj]))
(enable-console-print!)
(defrecord Result [title time])
(defn run-all [& tests]
(->>
(for [[title f] tests]
(let [start (js/Date.)]
(println "running:" title)
(dotimes [n 1000]
(let [result (f)]
(try
(assert (< 10000 (:a result)))
(catch js/Error e
(println "Err:" result)))))
(Result. title (- (js/Date.) start))))
(sort-by :time)
vec
clj->js
(.table js/console)))
(defn init []
(run-all
["get and set!"
(fn []
(let [obj #js{:a 0}]
(dotimes [x 10000]
(set! (.-a obj) (+ (.-a obj) (inc (rand-int 10)))))
(js->clj obj :keywordize-keys true)))]
["reduce immutable assoc"
(fn []
(reduce #(assoc % :a (+ (:a %) (inc (rand-int 10))))
{:a 0}
(range 10000)))]
["reduce immutable update"
(fn []
(reduce #(update % :a + (inc (rand-int 10)))
{:a 0}
(range 10000)))]
["dotimes volatile vswap!"
(fn []
(deref
(let [vol (volatile! {:a 0})]
(dotimes [x 10000]
(vswap! vol (fn [v] (update v :a + (inc (rand-int 10))))))
vol)))]
["reduce volatile vreset!"
(fn []
(deref
(let [vol (volatile! {:a 0})]
(dotimes [x 10000]
(vreset! vol {:a (+ (:a @vol) (inc (rand-int 10)))}))
vol)))]
["reduce volatile vreset! destructure"
(fn []
(deref
(let [vol (volatile! {:a 0})]
(dotimes [x 10000]
(let [{:keys [a]} @vol]
(vreset! vol {:a (+ a (inc (rand-int 10)))})))
vol)))]
["gobj get and set!"
(fn []
(let [obj #js{:a 0}]
(dotimes [x 10000]
(gobj/set obj "a" (+ (gobj/get obj "a") (inc (rand-int 10)))))
(js->clj obj :keywordize-keys true)))]
["transients"
(fn []
(let [obj (transient {:a 0})]
(dotimes [x 10000]
(assoc! obj :a (+ (:a obj) (inc (rand-int 10)))))
(persistent! obj)))]))
@minikomi

This comment has been minimized.

Copy link
Owner Author

@minikomi minikomi commented May 26, 2017

No Optimizations
screen shot 2017-05-26 at 13 43 43

Advanced optimization
screen shot 2017-05-26 at 13 44 40

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