Which is faster, Clojure or ClojureScript (and why)?

后端 未结 3 550
忘掉有多难
忘掉有多难 2021-02-02 13:08

If I had to guess, I\'m pretty sure the answer is Clojure, but I\'m not sure why. Logically (to me) it seems like ClojureScript should be faster:

Both are \"dynamic\",

3条回答
  •  迷失自我
    2021-02-02 13:47

    This question is hard to answer precisely, without reference to a specific benchmark task (or even specific versions of Clojure or ClojureScript).

    Having said that, in most situation I would expect Clojure to be somewhat faster. Reasons:

    • Clojure usually compiles down to static code, so it doesn't actually do any dynamic lookups at runtime. This is quite important: high performance code often produces bytecode that is very similar to statically typed Java. The question appears to be making the false assumption that a dynamic language has to do dynamic method lookups at runtime: this is not always the case (and usually isn't in Clojure)
    • The JVM JIT is very well engineered, and I believe it is currently still a bit better than the JavaScript JITs, despite how good V8 is.
    • If you need concurrency or need to take advantage of multiple cores then clearly there is no contest since JavaScript is single-threaded.....
    • The Clojure compiler is more mature than ClojureScript, and has had quite a lot of performance tuning work in recent years (including things like primitive support, protocols etc.)

    Of course, it is possible to write fast or slow code in any language. This will make more of a difference than the fundamental difference between the language implementations.

    And more fundamentally, your choice between Clojure and ClojureScript shouldn't be about performance in any case. Both offer compelling productivity advantages. The main deciding factor should be:

    • If you want to run on the web, use ClojureScript
    • If you want to run on the server in a JVM environnment, use Clojure

提交回复
热议问题