Xojo UUID Generator Class

Hi everyone,

I made a small utility class in Xojo that generates RFC 4122-compliant UUID v4 values.
Nothing fancy, but it might be useful if you need unique identifiers for things like databases, files, API keys, etc.

It supports:

  • Standard hexadecimal format (with or without hyphens)
  • Base64 format (with padding)
  • Easy to use, no dependencies

URL link to the project on the github site: Xojo-UUID-Generator-Class

Or Download direct : UUID Generator Class project.zip (12.1 KB)

18 Likes

could xojo include for all devs …

1 Like

Xojo should include UUID in the framework. V5 and V7 too.

7 Likes

I have a fairly robust class at Beacon/Project/Modules/Beacon/UUID.xojo_code at master · thommcgrath/Beacon · GitHub though I bet there are ways to make it faster.

5 Likes

I ran a test on a Mac Mini M2 Pro — this class takes 11 seconds to generate one million UUIDs, which I think is more than fast enough. Also, just because something looks more complex doesn’t mean it’s more efficient or robust — nothing beats simplicity.
I don’t understand why we should make it more complicated than that…

1 Like

Nice gift Fabrice.

Xojo should include this kind of thing in the framework, but until then, yours or Thoms are great open resources.

Tim

1 Like

Thanks Fabrice.

Henry

1 Like

Adding my module to the list. It really does the same as everything else, just with syntax I like. :smile:

2 Likes

Thought I’d add an implementation I made that relies on an in-memory SQLite database with a view.

3 Likes

Just curious did you benchmark this against something generating uuids using “regular” Xojo code?

I didn’t. I don’t typically use it this way. I include SELECT next FROM uuid7 in my database insertion statements to generate row IDs using UUIDv7 values. Thought it would make a fun and interesting contribution, though.

Here’s an example of using it in a query and returning the generated UUIDv7 ID value:

Private Function writeName(name as String) As String
  var rs as RowSet = db.SelectSQL( "INSERT INTO names (id, name) VALUES ((SELECT next FROM uuid7), ?) RETURNING id;", name )
  if rs <> nil and rs.AfterLastRow = False then Return rs.Column( "id" ).StringValue
End Function

names table schema:

CREATE TABLE "names" ("id" TEXT PRIMARY KEY NOT NULL UNIQUE, "name" TEXT NOT NULL)

Just extract the SQL from the kTrigger constant and add that view to your database. Used in this way, it may be faster than generating the UUID in Xojo code then inserting the row as everything is offloaded to the DB.

1 Like

Yes that’s a very handy and interesting way of doing it.

Just did a quick benchmark and the result doesn’t really surprise me as SQLite uses a plugin which is generally faster than xojo code.

  • Generating 1.000.000 UUIDs with SQLite:
    13 seconds

  • Generating 1.000.000 UUIDs with Beacon.UUID:
    17 seconds

Although the performance difference when generating a single UUID is negligible.

4 Likes

I’m actually surprised it performed as well as it did. I didn’t exactly write it with performance in mind.

1 Like