Skip to content

Commit

Permalink
perf: replace md5 package with node:crypto
Browse files Browse the repository at this point in the history
This is a 3x speedup on my computer.
  • Loading branch information
stefansundin authored and jmcdo29 committed Feb 9, 2024
1 parent 7a4a9ed commit 8951d73
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 38 deletions.
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
"prepublishOnly": "pinst --disable",
"release": "changeset publish"
},
"dependencies": {
"md5": "^2.2.1"
},
"devDependencies": {
"@apollo/server": "4.10.0",
"@changesets/cli": "2.27.1",
Expand All @@ -67,7 +64,6 @@
"@types/express": "4.17.21",
"@types/express-serve-static-core": "4.17.43",
"@types/jest": "29.5.12",
"@types/md5": "2.3.5",
"@types/node": "20.11.17",
"@types/supertest": "6.0.2",
"@typescript-eslint/eslint-plugin": "6.21.0",
Expand Down
32 changes: 0 additions & 32 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as crypto from 'node:crypto';

export function md5(text: string): string {
return crypto.createHash('md5').update(text).digest('hex');
}
2 changes: 1 addition & 1 deletion src/throttler.guard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import * as md5 from 'md5';
import { md5 } from './hash';
import {
Resolvable,
ThrottlerGenerateKeyFunction,
Expand Down
2 changes: 1 addition & 1 deletion test/function-overrides/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ExecutionContext, Module } from '@nestjs/common';
import { APP_GUARD } from '@nestjs/core';
import { ThrottlerGuard, ThrottlerModule, seconds } from '../../src';
import { FunctionOverridesThrottlerController } from './function-overrides-throttler.controller';
import md5 = require('md5');
import { md5 } from '../utility/hash';
import assert = require('assert');

@Module({
Expand Down
5 changes: 5 additions & 0 deletions test/utility/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as crypto from 'node:crypto';

export function md5(text: string): string {
return crypto.createHash('md5').update(text).digest('hex');
}

0 comments on commit 8951d73

Please sign in to comment.