ClojureScript map lookup slow

与世无争的帅哥 提交于 2019-12-05 18:20:18

On my machine running your exact example with advanced compilation takes ~14ms on my 1.7ghz Macbook Air running a relatively recent v8 built from source.

To make sure we're benchmarking what we think we're benchmarking it's best to write something like this:

(let [v [1 2 8]]
  (dotimes [_ 5]
    (time
      (dotimes [_ 500000]
        (get my-map v)))))

On my machine this takes ~70ms on machine for Clojure JVM. ClojureScript runs this around ~3600ms, so about 50X slower. Why? It's because we default to PersistentArrayMap where Clojure does not when defining small hash maps with complex keys.

What happens if we define my-map like this instead:

(def my-map
  (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]))

The benchmark then takes ~170ms, which isn't so far off from Clojure JVM.

So there's definitely plenty of optimizations that Clojure implements that we haven't gotten around to yet. Still I'd say that for idiomatic Clojure code I think the best we can hope for on highly tuned JavaScript engines like V8 is 2-10X of Clojure JVM..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!