Skip to main content

Command Palette

Search for a command to run...

Why you should ditch NPM UUID?

Faster and native, NodeJS has you covered!

Updated
โ€ข2 min read
Why you should ditch NPM UUID?
P

I'm Pierre-Henry Soria. A passionate full stack engineer, building things that matter, making a real impact on the world.

Really like to take care of others and manage my workflow based on productivity methodologies.

Open to fast-paced changes with rapidly evolving business and technologies. I'm always thirsty to learn and undertake new exciting things and thrilling challenges.

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

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

crypto.randomUUID(): 518.735ms
uuid.v4(): 532.768ms

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

Note

crypto.randomUUID() only generates UUIDv4 (random UUIDs), not UUIDv7 for instance. To generate UUIDv7, you need to use a library like uuid.


More from this blog

Super Passionate Software Engineer

37 posts

Pierre-Henry Soria. A super passionate & enthusiastic software engineer! ๐Ÿš€ True cheese & chocolate lover! ๐Ÿ˜‹ ๐Ÿ‘‰ Reach me at https://ph7.me ๐Ÿ’ซ