CLJS: UUID generator for ClojureScript

1,755 views
Skip to first unread message

Frank Siebenlist

unread,
Nov 29, 2012, 1:25:55 AM11/29/12
to Clojure, Frank Siebenlist
I need UUIDs in my CLJS code…

cljs.core does include a UUID type, but no generator.

I found a couple of efforts and example code at https://github.com/davesann/cljs-uuid and http://catamorphic.wordpress.com/2012/03/02/generating-a-random-uuid-in-clojurescript,
but they didn't work with cljs.core/UUID or were too slow.

There are many javascript versions out there, but that would require another external js-lib.
(really surpising that I couldn't find a UUID generator in closure-lib…)

Please take a look at my UUID-playground at "https://gist.github.com/4159427",
which includes a few implementations with some rudimentary timing results.

Appreciate any comments or suggestions, or maybe a pointer to a faster, cleaner implementation...

Thanks, FrankS.

PS. I would think that the cljs-community would love to have a UUIDv4 generator as part of the clojurescript distro...


David Nolen

unread,
Nov 29, 2012, 11:27:39 AM11/29/12
to clojure
closures inside the body of a function are not free in JS. I would lift those helpers out. In general I see no benefit to writing your "fast" code in JS - all the facilities for writing efficient code are available in ClojureScript itself. I agree that it's not completely clear what subset of ClojureScript is guaranteed to be efficient - hopefully somebody will take the time to try some experiments and document them, I'm more than willing to help anyone that attempts to do so.

David




--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

David Nolen

unread,
Nov 29, 2012, 11:36:40 AM11/29/12
to clojure
Oh though before you lift them out by hand - I would double check that :simple optimizations doesn't already do this for you :)


On Thu, Nov 29, 2012 at 1:25 AM, Frank Siebenlist <frank.si...@gmail.com> wrote:

Frank Siebenlist

unread,
Nov 29, 2012, 1:07:17 PM11/29/12
to clo...@googlegroups.com, Frank Siebenlist
Thanks for the feedback!

Defining the two helper functions outside of the function-scope doesn't seem to have any effect on the performance numbers.

…but I have to confess that all testing was done at the repl without any optimization so far…

-FS.

David Nolen

unread,
Nov 29, 2012, 1:20:39 PM11/29/12
to clojure
Also note that testing with the Rhino REPL is not informative about performance in anyway - you absolutely need to test your code against the modern JS engines - V8, JavaScriptCore, or SpiderMonkey (with JIT turned on). For code like this they are often 100X faster if not far greater than that.

Frank Siebenlist

unread,
Dec 2, 2012, 9:36:55 PM12/2/12
to clo...@googlegroups.com, Frank Siebenlist
I've wrapped my random uuid generator up in a little library at:

https://github.com/franks42/cljs-uuid-utils

---
cljs-uuid-utils

ClojureScript micro-library with an implementation of a type 4, random UUID generator compatible with RFC-4122 and cljs.core/UUID (make-random-uuid), a uuid-string conformance validating predicate (valid-uuid?), and a UUID factory from uuid-string with conformance validation (make-uuid-from).
---

As mentioned before, clojure on the jvm can leverage the ubiquitous "java.util.UUID/randomUUID", while there is no generally available random uuid generator in the javascript world. Although this micro-lib addresses that issue somewhat, I believe that we should have a random-uuid generator as part of the batteries-included cljs-distro.

Another issue that I came across is the fact that the java.util.UUID/fromString does do a conformance check of the presented uuid-string, and throws an exception if it does not pass. ClojureScript's reader yields a cljs.core/UUID instance for uuid-literals, but doesn't check for any conformance and essentially accepts any string. This could lead to interesting, surprising bugs during cljs-clj interop.

Please let me know if you use this little library, and any suggestions/comments for improvement are always welcome.

Enjoy, FrankS.

Frank Siebenlist

unread,
Dec 3, 2012, 8:20:58 PM12/3/12
to clo...@googlegroups.com, Frank Siebenlist
Added a "uuid-string" getter function to obtain a string representation of a UUID-instance, which makes for easier interop with apps that require such a uuid-string, like web-app, json, databases (i.e. legacy apps that have not been made EDN-aware yet ;-) )

(thanks to Robert Stuttaford for the idea!)

Bumped version to [cljs-uuid-utils "0.1.3"]

-FS.
Reply all
Reply to author
Forward
0 new messages