Skip to content

Instantly share code, notes, and snippets.

@Deraen
Created November 10, 2022 18:45
Show Gist options
  • Save Deraen/361c105108fa96c4ae7de58f3a85946e to your computer and use it in GitHub Desktop.
Save Deraen/361c105108fa96c4ae7de58f3a85946e to your computer and use it in GitHub Desktop.
Testing React vs Reagent update latency
(def start (atom nil))
(def log (atom []))
(defn latency-test []
(let [a (r/atom 0)]
(fn []
(let [[b update-b] (react/useState 0)]
(react/useEffect
(fn []
(let [now (js/performance.now)
d (- now @start)]
(swap! log (fn [log]
(let [log (conj log d)]
(if (>= (count log) 100)
(do
(js/console.log (str/join "\n" log))
[])
log)))))
js/undefined))
[:div
[:button
{:type "button"
:on-click (fn [e]
(reset! start (js/performance.now))
(swap! a inc))}
"ratom"]
[:button
{:type "button"
:on-click (fn [e]
(reset! start (js/performance.now))
(update-b inc))}
"hook"]
b
@a]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment