I have a simple map:
(def my-map
{[1 2 3] 1
[1 2 4] 5
[3 4 2] 3
[4 5 3] 3
[5 2 5] 6
[9 2 1] 5
[8 3 1] 6})
that I use for performing lookups. This performs rather poorly, however:
(time (doseq [x (range 500)]
(my-map [1 2 8])))
"Elapsed time: 170 msecs"
On the same machine, Clojure can do 500,000 in about 236 msecs, or about 700x faster. While it's not unexpected for Clojure to be faster than ClojureScript, I'm confused why ClojureScript would be so much slower.
Any ideas about how I could make a simple multi-valued lookup map as above efficiently and in a readable manner in ClojureScript? I know doing a bunch of if
s instead of using a vector-key solution would certainly work faster, but I'm looking at something that's somewhat more readable / maintainable.
Just to update with more information. The above was done in Firefox, so thus slower than compared to V8. The following:
(def my-map2
(into cljs.core.PersistentHashMap/EMPTY
{[1 2 3] 1
[1 2 4] 5
[3 4 2] 3
[4 5 3] 3
[5 2 5] 6
[9 2 1] 5
[8 3 1] 6}))
(defn p1 []
(let [v [1 2 8]]
(dotimes [_ 5]
(time (dotimes [_ 500000]
(get my-map2 v))))))
gives:
"Elapsed time: 3295 msecs"
"Elapsed time: 3246 msecs"
"Elapsed time: 3113 msecs"
"Elapsed time: 3107 msecs"
"Elapsed time: 3121 msecs"
in Chromium Version 25.0.1364.160 Ubuntu 13.04 (25.0.1364.160-0ubuntu3). So still about 13x times slower in ClojureScript than Clojure, but that's much better than what it was before. Note also that I'm running this directly in browser repl.