ClojureScript and WebGL, trying to generate large javascript arrays (which is really slow)

369 views
Skip to first unread message

Andreas Arnold

unread,
Jun 2, 2014, 2:50:58 AM6/2/14
to clojur...@googlegroups.com
I'm trying to use ClojureScript to write a tile-based visualization with WebGL. This requires me to build up a large JavaScript array.

As you can see in the attached screenshot, this leads to a small problem: `clj->js` is really slow (this was run with a grid of 100x100).

Annotating the arrays with '#js' won't work since they are generated dynamically. Making the arrays offline is, as I see it, not an option either, since the colors of the tiles should change based on input, so one large JavaScript array at least is needed during the execution.

I was wondering if there is some other workaround for this? Or did I just stumble upon a use case where ClojureScript might not be a good solution?

cljjs.png

David Nolen

unread,
Jun 2, 2014, 6:57:43 AM6/2/14
to clojur...@googlegroups.com
You can construct JS arrays quickly just like you would in JavaScript.

David



--
Note that posts from new members are moderated - please be patient with your first post.
---
You received this message because you are subscribed to the Google Groups "ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojurescrip...@googlegroups.com.
To post to this group, send email to clojur...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojurescript.

Andreas Arnold

unread,
Jun 2, 2014, 9:52:57 AM6/2/14
to clojur...@googlegroups.com
You mean with `#js`? For example (def thing-one #js [1 2 3])? That way I can't use any ClojureScript methods like `map`, right?

A example might help to really figure out what I am trying to do here. I have code like the following:

(defn build-tile-vertices [x-coord y-coord]
(let [tile-template [[0 0 0]
[1 0 0]
[1 1 0]
[0 1 0]]
translate (fn [[x y z]] [(+ x x-coord) (+ y y-coord) z])]
(mapcat translate tile-template)))

Would there be a way to keep all of this directly as a JavaScript array? Or did you mean I should fall back to bare JavaScript? Or is there something totally different that I am missing?

David Nolen

unread,
Jun 2, 2014, 10:43:31 AM6/2/14
to clojur...@googlegroups.com
If eeking out every last bit of performance is your #1 concern, then no you should not use ClojureScript functional operations over primitive arrays. loop/recur, aget, aset and standard Array methods are your friends.

David

Andreas Arnold

unread,
Jun 3, 2014, 4:32:18 PM6/3/14
to clojur...@googlegroups.com
Thanks David!

I tried it today with `aset` and more 'basic' JavaScript and got it down to 50ms.


You received this message because you are subscribed to a topic in the Google Groups "ClojureScript" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojurescript/Z2pFr_qy79w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojurescrip...@googlegroups.com.

Yehonathan Sharvit

unread,
Jun 4, 2014, 4:23:01 PM6/4/14
to clojur...@googlegroups.com
You can also use amap and reduce to keep the functional spirit alive.

http://clojuredocs.org/clojure_core/clojure.core/amap
http://clojuredocs.org/clojure_core/clojure.core/areduce
Reply all
Reply to author
Forward
0 new messages