Fork me on GitHub
#malli
<
2019-10-09
>
ikitommi08:10:53

ok, could be faster. the date parsing on java-side seems quite slow in general:

(require '[malli.core :as m])
(require '[malli.transform :as mt])

(def Pizza
  [:map
   [:id int?]
   [:name string?]
   [:created inst?]])

(def json->Pizza
  (m/transformer
    Pizza
    mt/json-transformer))


(json->Pizza
  {:id 1
   :name "salami"
   :created "2019-10-04T09:08:36.078Z"})
;{:id 1
; :name "salami"
; :created #inst"2019-10-04T09:08:36.078-00:00"}

(require '[criterium.core :as cc])

(cc/quick-bench
  (json->Pizza
    {:id 1
     :name "salami"
     :created "2019-10-04T09:08:36.078Z"}))
; Evaluation count : 386256 in 6 samples of 64376 calls.
;              Execution time mean : 1.719360 µs
;     Execution time std-deviation : 100.090402 ns
;    Execution time lower quantile : 1.601860 µs ( 2.5%)
;    Execution time upper quantile : 1.811632 µs (97.5%)
;                    Overhead used : 1.862824 ns

ikitommi08:10:34

oh, Plumatic Schema doesn’t have date coercion oob, forgot about that.

ikitommi08:10:59

with schema-tools:

(require '[schema.core :as s])
(require '[schema-tools.coerce :as stc])

(def json->Pizza2
  (stc/coercer {:id s/Int, :name s/Str, :created s/Inst} stc/json-coercion-matcher))

(json->Pizza2
  {:id 1
   :name "salami"
   :created "2019-10-04T09:08:36.078Z"})
;{:id 1
; :name "salami"
; :created #inst"2019-10-04T09:08:36.078-00:00"}

(cc/quick-bench
  (json->Pizza2
    {:id 1
     :name "salami"
     :created "2019-10-04T09:08:36.078Z"}))
; Evaluation count : 261396 in 6 samples of 43566 calls.
;              Execution time mean : 2.352737 µs
;     Execution time std-deviation : 173.369968 ns
;    Execution time lower quantile : 2.255370 µs ( 2.5%)
;    Execution time upper quantile : 2.636725 µs (97.5%)
;                    Overhead used : 1.600826 ns

👍 4