Created
November 23, 2017 13:21
-
-
Save rauhs/c2f84c91ef311c1c080a93b852c50daa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def kw-cache #js{}) | |
(defn cached-kw [fqn] | |
(if-some [kw (unchecked-get kw-cache fqn)] | |
kw | |
(let [kw (keyword fqn)] | |
(unchecked-set kw-cache fqn kw) | |
kw))) | |
(defn js->clj-fast | |
[x] | |
(let [f (fn thisfn [x] | |
(case (js* "typeof ~{}" x) | |
"string" x | |
"number" x | |
"boolean" x | |
"object" | |
(cond | |
(array? x) | |
(if (<= (alength x) 32) | |
(.fromArray PersistentVector | |
(amap x idx ret (aset ret idx (thisfn (aget x idx)))) | |
true) | |
(mapv thisfn x)) | |
(identical? (.-constructor x) js/Object) | |
(let [out (array)] | |
(js* "for (var key in ~{}) {~{}.push(~{}(key)); ~{}.push(~{}(~{}[key]));}" | |
x out cached-kw out thisfn x) | |
(if (<= (alength out) 16) | |
(.fromArray PersistentArrayMap out true true) | |
(.fromArray PersistentHashMap out true))) | |
:else x) | |
x))] | |
(f x))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not for node (due to keyword cache). Almost not tested.