DEV Community

Cover image for Why you should ditch NPM UUID. Node has you covered!
Pierre-Henry Soria ✨
Pierre-Henry Soria ✨

Posted on • Edited on

6 2 1 2

Why you should ditch NPM UUID. Node has you covered!

In this post, you will see the reason why you shouldn't use anymore the uuid NPM package anymore for generating Universally Unique Identifiers.

If you have worked income important backend NodeJS applications, you are probably familiar with the uuid package. However, there is a less common, but core Node module called crypto, available since NodeJS v14.17.

According to my benchmark, the core module randomUUID() from node:crypto performs better and quicker in terms of speed and reliability. Additionally, it is natively accessible in Node.js since v14.17 without the necessity of installing a 3rd-party package like uuid.

Benchmark UUID VS Crypto RandomUUID

Note: My benchmark was run with Node v18.18.1

// Run: node benchmark-uuid-vs-core-crypto.js

// native randomUUID crypto benchmark
const { randomUUID } = require("crypto");

console.time("crypto.randomUUID()");
for (let time = 0; time < 10_000_000; time++) {
  randomUUID();
}

console.timeEnd("crypto.randomUUID()");

// uuid package benchmark
const { v4 } = require("uuid");
console.time("uuid.v4()");
for (let time = 0; time < 10_000_000; time++) {
  v4();
}
console.timeEnd("uuid.v4()");

// note, I've used 10_000_000 with _ which are numeric separators
// https://github.com/pH-7/GoodJsCode#-clearreadable-numbers
Enter fullscreen mode Exit fullscreen mode

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (2)

Collapse
 
aberba profile image
Lawrence Aberba

The re several version of UUID v3, v5,... v7 whihc uuid package supports

Collapse
 
pierre profile image
Pierre-Henry Soria ✨ • Edited

I can't wait to hear your thoughts and options! 😊 Share them by commenting on this article 🤗

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay