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

clojure, clojurescript, v8

Solution

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

Problem

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", but ClojureScript - Compiles to JavaScript, running on V8 - V8 engine is arguably the fastest dynamic language engine there is - V8 is written in C whereas Clojure: - Is also dynamic - Runs in JVM, which has no built-in dynamic support, so I'm thinking thus JVM has to do whatever V8 is doing too, to enable dynamic support - and Java is slower than C So how could Clojure be faster than ClojureScript? Does "dynamic" mean something different when saying JavaScript is dynamic and Clojure is dynamic? What am I not seeing? (Of course if ClojureScript is indeed faster, then is the above reasoning correct?) I guess, what does Clojure compile to....is at least part of the question. I know the JVM part can't just be a plain interpreter (otherwise ClojureScript would be faster), but Clojure can't compile to regular bytecode, as there's no "dynamic" in the JVM. So what's the difference between how ClojureScript is compiled/executed and how Clojure is compiled/excecuted and how plain Java is compiled/executed, and the performance differences implied in each?

Original source