UUID creation in Postgres vs Node

March 24, 2017
--

I was making a new Postgres database table and I decided to do UUID primary keys instead of auto-incremented integers as I had done in the past. There are some interesting discussions around why UUIDs are a good idea in spite of their increased size.

When I wrote the query for insertion, I defaulted to letting Postgres do the work for UUID creation.

INSERTINTOusers(id,username)VALUES(uuid_generate_v4(),'newtang');

I didn’t have a good reason for doing this other than the habit of letting the database handle the primary key creation. But, the more I thought about it, the more it made sense to generate the UUID in Node (this was a Node.JS, Postgres stack). I wouldn’t have to wait for the Postgres INSERT query to complete to use the generated UUID elsewhere. If I needed this UUID in multiple places, I could fire off several queries at once or more easily build out a transaction. But, is there a performance difference between the two? I’m not a great Postgres guy, but with the help from Stack Overflow, I cobbled together this method.

I’m using the uuid npm module. The console.time and console.timeEnd are built-in, handy ways to measure duration in Javascript without having to build Date objects and comparing. Anywho, the results:

4792.588ms4809.111ms4709.646ms

Dang, we’re talking 10x faster! Granted, this isn’t a perfect test. I ran this on my Mac laptop which isn’t going to have the same hardware as a server. But, it’s hard to imagine the disparity being that much different on different hardware. Also, my project will probably never involve cranking out rapid fire UUIDs at this rate, but it’s fascinating nonetheless. Time to create some UUIDs in Node!