mirror of
https://github.com/metosin/reitit.git
synced 2025-12-18 08:51:12 +00:00
tune perf
This commit is contained in:
parent
6c23a5562a
commit
682dd05568
2 changed files with 64 additions and 13 deletions
|
|
@ -203,19 +203,14 @@
|
||||||
|
|
||||||
(defn form-encode [s]
|
(defn form-encode [s]
|
||||||
(if s
|
(if s
|
||||||
#?(:clj (-> s
|
#?(:clj (URLEncoder/encode ^String s "UTF-8")
|
||||||
(str/replace #"[^A-Za-z0-9\!'\(\)\*_~.-\\\ ]+" percent-encode)
|
|
||||||
(^String .replace " " "+"))
|
|
||||||
:cljs (str/replace (js/encodeURIComponent s) "%20" "+"))))
|
:cljs (str/replace (js/encodeURIComponent s) "%20" "+"))))
|
||||||
|
|
||||||
(defn form-decode [s]
|
(defn form-decode [s]
|
||||||
(if s
|
(if s
|
||||||
#?(:clj (let [s (if (.contains ^String s "+")
|
#?(:clj (if (or (.contains ^String s "%") (.contains ^String s "+"))
|
||||||
(.replace ^String s "+" " ")
|
(URLDecoder/decode ^String s "UTF-8")
|
||||||
s)]
|
s)
|
||||||
(if (.contains ^String s "%")
|
|
||||||
(URLDecoder/decode s "UTF-8")
|
|
||||||
s))
|
|
||||||
:cljs (js/decodeURIComponent (str/replace s "+" " ")))))
|
:cljs (js/decodeURIComponent (str/replace s "+" " ")))))
|
||||||
|
|
||||||
(defprotocol IntoString
|
(defprotocol IntoString
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@
|
||||||
(.replace ^String s "+" "%2B")
|
(.replace ^String s "+" "%2B")
|
||||||
"UTF-8"))
|
"UTF-8"))
|
||||||
|
|
||||||
(defn decode! []
|
(defn url-decode! []
|
||||||
|
|
||||||
;; ring
|
;; ring
|
||||||
|
|
||||||
|
|
@ -77,7 +77,7 @@
|
||||||
(.contains s "(") (.replace "%28" "(")
|
(.contains s "(") (.replace "%28" "(")
|
||||||
(.contains s ")") (.replace "%29" ")")))
|
(.contains s ")") (.replace "%29" ")")))
|
||||||
|
|
||||||
(defn encode! []
|
(defn url-encode! []
|
||||||
|
|
||||||
;; ring
|
;; ring
|
||||||
|
|
||||||
|
|
@ -111,6 +111,62 @@
|
||||||
"1"]]
|
"1"]]
|
||||||
(test! f s))))
|
(test! f s))))
|
||||||
|
|
||||||
|
(defn form-decode! []
|
||||||
|
|
||||||
|
;; ring
|
||||||
|
|
||||||
|
;; 280ns
|
||||||
|
;; 130ns
|
||||||
|
;; 43ns
|
||||||
|
;; 25ns
|
||||||
|
|
||||||
|
;; reitit
|
||||||
|
|
||||||
|
;; 270ns (-4%)
|
||||||
|
;; 20ns (-84%)
|
||||||
|
;; 47ns (+8%)
|
||||||
|
;; 12ns (-52%)
|
||||||
|
|
||||||
|
(doseq [fs ['ring.util.codec/form-decode-str
|
||||||
|
'reitit.impl/form-decode]
|
||||||
|
:let [f (deref (resolve fs))]]
|
||||||
|
(suite (str fs))
|
||||||
|
(doseq [s ["%2Baja%20hiljaa+sillalla"
|
||||||
|
"aja_hiljaa_sillalla"
|
||||||
|
"1+1"
|
||||||
|
"1"]]
|
||||||
|
(test! f s))))
|
||||||
|
|
||||||
|
(defn form-encode! []
|
||||||
|
|
||||||
|
;; ring
|
||||||
|
|
||||||
|
;; 240ns
|
||||||
|
;; 120ns
|
||||||
|
;; 130ns
|
||||||
|
;; 31ns
|
||||||
|
|
||||||
|
;; reitit
|
||||||
|
|
||||||
|
;; 210ns
|
||||||
|
;; 120ns
|
||||||
|
;; 130ns
|
||||||
|
;; 30ns
|
||||||
|
|
||||||
|
(doseq [fs ['ring.util.codec/form-encode
|
||||||
|
'reitit.impl/form-encode]
|
||||||
|
:let [f (deref (resolve fs))]]
|
||||||
|
(suite (str fs))
|
||||||
|
(doseq [s ["aja hiljaa+sillalla"
|
||||||
|
"aja_hiljaa_sillalla"
|
||||||
|
"1+1"
|
||||||
|
"1"]]
|
||||||
|
(test! f s))))
|
||||||
|
|
||||||
(comment
|
(comment
|
||||||
(decode!)
|
(url-decode!)
|
||||||
(encode!))
|
(url-encode!)
|
||||||
|
(form-decode!)
|
||||||
|
(form-encode!))
|
||||||
|
|
||||||
|
(ring.util.codec/form-decode-str "%2B632+905+123+4567")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue