squint-cljs / cherry

Experimental ClojureScript to ES6 module compiler
https://squint-cljs.github.io/cherry
558 stars 22 forks source link

optimize zero? #93

Closed borkdude closed 1 year ago

borkdude commented 1 year ago

When compiling (zero? ..) we can inline the implementation and skip the function call.

That should speed up this output:

$ node out/eval.js -e '(time (print (loop [i 0 j 10000000] (if (zero? j) i (recur (inc i) (dec j))))))'
10000000
"Elapsed time: 132.894250 msecs"

I think it becomes slower due to:

(cljs.core.truth_(cljs.core.zero_QMARK_.call(null, j2)))

CLJS compiles this directly into:

cljs.user=> (prn (str (fn [x] (if (zero? x) 1 2))))
"function (x){\nif((x === (0))){\nreturn (1);\n} else {\nreturn (2);\n}\n}"

So the truth_ check is omitted and also the zero? function call.