Skip to content

Instantly share code, notes, and snippets.

@H4ad
Created January 8, 2023 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save H4ad/0d4efdf2b1ccd2f944419671aabb9db2 to your computer and use it in GitHub Desktop.
Save H4ad/0d4efdf2b1ccd2f944419671aabb9db2 to your computer and use it in GitHub Desktop.
NestJS UUID to UID
// to run this file, install: npm i --save benchmark uuid uid nanoid@3.x
// and then just run with: npx ts-node ids-benchmark.ts
import * as Benchmark from 'benchmark';
import { uid } from 'uid';
import * as uuid from 'uuid';
import * as nanoid from 'nanoid';
const suite = new Benchmark.Suite();
suite.add('uuid', function () {
const id = uuid.v4();
});
suite.add('nanoid', function () {
const id = nanoid.nanoid();
});
suite.add('uid', function () {
const id = uid(21);
});
suite
// add listeners
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run({
async: true,
});
// this file I use to get the profiler information
// to run this file, compile to JavaScript first,
// then you run: node --prof dist/perf-startup-10000.js
// this will generate a file called: isolate-0xnnnnnnnnnnnn-v8.log
// to be able to read the information inside this file, run:
// node --prof-process isolate-0xnnnnnnnnnnnn-v8.log > processed.txt
import { AppModule } from '../src/app.module';
import { NestFactory } from '@nestjs/core';
import * as microtime from 'microtime';
async function main() {
const start = microtime.now();
for (let i = 0; i < 1e4; i++)
await NestFactory.create(AppModule, { logger: false });
const end = microtime.now();
console.log(`Diff: ${end - start}`);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment