Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 19, 2021 12:36 am GMT

crypto.randomUUID is three times faster uuid.v4

Node.js v14.17 release added crypto.randomUUID(). This method allows to generate random RFC 4122 Version 4 UUID strings. Example:

const { randomUUID } = require('crypto');console.log(randomUUID());// '43c98ac2-8493-49b0-95d8-de843d90e6ca'

I wondered how big the difference between uuid generation by Node.js API and uuid package.

For benchmarking I prefer to use hyperfine. It is like apache benchmark, but for CLI commands. There are have two cases:

  1. require('crypto').randomUUID()
  2. require('uuid').v4()

Let's put them into two files:

// test-native.jsconst { randomUUID } = require('crypto');for (let i = 0; i < 10_000_000; i++) {  randomUUID();} 
// test-uuid.jsconst { v4 } = require('uuid');for (let i = 0; i < 10_000_000; i++) {  v4();}

Now we ready for benchmarking:
hyperfine 'node test-native.js' 'node test-uuid.js'

This command shows that native generation is three times faster than uuid package. Awesome!


Original Link: https://dev.to/galkin/crypto-randomuuid-vs-uuid-v4-47i5

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To