metosin / sieppari

Small, fast, and complete interceptor library for Clojure/Script
Eclipse Public License 2.0
207 stars 21 forks source link

Context, Stack and Queue as a Protocols #18

Open ikitommi opened 5 years ago

ikitommi commented 5 years ago

Using protocols instead of Context records/maps would allow a) much faster dispatching and b) sieppari to be used with plain objects (with static queues) too:

(let [interceptors (mapv interceptor [{:enter inc
                                       :leave inc}
                                      {:enter (partial * 2)
                                       :leave (partial * 2)}
                                      {:enter inc}])]
  ;; 100ns
  (cc/quick-bench
    (run (queue interceptors) 0)))

(let [app (comp inc (partial * 2) inc (partial * 2) inc)]
  ;; 88ns
  (cc/quick-bench
    (app 0)))