Skip to content

Instantly share code, notes, and snippets.

@rauhs
Created November 23, 2017 13:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rauhs/c2f84c91ef311c1c080a93b852c50daa to your computer and use it in GitHub Desktop.
Save rauhs/c2f84c91ef311c1c080a93b852c50daa to your computer and use it in GitHub Desktop.
(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)))
@rauhs
Copy link
Author

rauhs commented Nov 23, 2017

Not for node (due to keyword cache). Almost not tested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment