An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
May 19, 2021 12:36 am GMT
Original Link: https://dev.to/galkin/crypto-randomuuid-vs-uuid-v4-47i5
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:
- require('crypto').randomUUID()
- 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:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To