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})
which I use to perform a search. However, this does pretty poorly:
(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 ms, or about 700 times faster. Although it is not unexpected for Clojure to be faster than ClojureScript, I am confused why ClojureScript will be much slower.
Any ideas on how I could make a simple multi-valued search map, as shown above, efficiently and readable in ClojureScript? I know that making a bunch of if instead of using a solution with a vector key will certainly work faster, but I'm looking at something more readable / supported.
Just update the additional information. The foregoing was done in Firefox, so itβs slower than V8. 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 it's about 13 times slower in ClojureScript than Clojure, but much better than before. Also note that I run this directly in the replica of the browser.
source share