Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does using nth in clojurescript increase my codesize by 74026%

I have been comparing the generated javascript outputed by various calls in clojurescript and it feels like stepping on landmines. Some generate extremely readable (even in minified advanced mode) javascript and some decide this one method call is going to require what seems like every possible method in clojure to run.

nth vs aget is a very good example of this. Both of these code snippets output the number 5 but one takes 77 bytes to do it and the other takes 57 kilobytes. Thats an increase of 74026%

57 KiloBytes - nth

(ns fooModule)
(let [log js/console.log
    x (array 5)]
    (log (nth x 0)))

77 Bytes - aget

(ns fooModule)
(let [log js/console.log
    x (array 5)]
    (log (aget x 0)))

The code generated by aget is very readable. (formatting put in by hand)

;(function(){
    var a=console.log,
        b=[5];
    a.a ? a.a(b[0]) : a.call( null, b[0] );
})();

generated nth code in a gist https://gist.github.com/trashhalo/7781298\

clojurescript 0.0-2014

like image 500
Stephen Solka Avatar asked Dec 04 '13 02:12

Stephen Solka


1 Answers

In this case It's due to dead code elimination interacting with ClojureScrip's sequences. Your example just happens to use exactly one thing that uses sequences, nth and nothing else. So when in the first example the seq library is being dropped and in the second it is not. Don't generalize this growth rate, there is only about that much of ClojureScript total so once you use any code that includes it, you will have figurativly stepped on all the mines.

like image 137
Arthur Ulfeldt Avatar answered Sep 23 '22 09:09

Arthur Ulfeldt