Skip to content

Commit 106f94a

Browse files
committed
refactor: improve static find performance
1 parent 0818c36 commit 106f94a

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

lib/helpers/initialize_clients.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@ import instance from './weak_cache.js';
22
import isPlainObject from './_/is_plain_object.js';
33
import { InvalidClientMetadata } from './errors.js';
44

5-
function addStatic(metadata) {
6-
const { staticClients } = instance(this);
7-
if (!isPlainObject(metadata) || !metadata.client_id) {
8-
throw new InvalidClientMetadata('client_id is mandatory property for statically configured clients');
9-
}
5+
export default function initializeClients(clients = []) {
6+
let staticClients;
107

11-
if (staticClients.has(metadata.client_id)) {
12-
throw new InvalidClientMetadata('client_id must be unique amongst statically configured clients');
13-
}
8+
for (const metadata of clients) {
9+
if (!isPlainObject(metadata) || !metadata.client_id) {
10+
throw new InvalidClientMetadata('client_id is mandatory property for statically configured clients');
11+
}
1412

15-
staticClients.set(metadata.client_id, structuredClone(metadata));
16-
}
13+
if (staticClients?.has(metadata.client_id)) {
14+
throw new InvalidClientMetadata('client_id must be unique amongst statically configured clients');
15+
}
1716

18-
export default function initializeClients(clients = []) {
19-
clients.map(addStatic, this);
17+
staticClients ||= new Map();
18+
staticClients.set(metadata.client_id, structuredClone(metadata));
19+
}
20+
21+
if (staticClients) {
22+
instance(this).staticClients = staticClients;
23+
}
2024
}

lib/models/client.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -515,25 +515,25 @@ export default function getClient(provider) {
515515
}
516516

517517
static async find(id) {
518-
if (typeof id !== 'string') {
518+
if (typeof id !== 'string' || !id.length) {
519519
return undefined;
520520
}
521521

522522
const { staticClients, dynamicClients } = instance(provider);
523523

524-
if (staticClients.has(id)) {
525-
const cached = staticClients.get(id);
526-
524+
const cached = staticClients?.get(id);
525+
if (cached) {
527526
if (!(cached instanceof Client)) {
528527
const client = new Client(cached);
529528
if (client.sectorIdentifierUri !== undefined) {
530529
await sectorValidate(provider, client);
531530
}
532531
Object.defineProperty(client, 'noManage', { value: true });
533532
staticClients.set(id, client);
533+
return client;
534534
}
535535

536-
return staticClients.get(id);
536+
return cached;
537537
}
538538

539539
const properties = await this.adapter.find(id);

lib/provider.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export class Provider extends Koa {
6262
#exec;
6363

6464
#int = {
65-
staticClients: new Map(),
6665
dynamicClients: new QuickLRU({ maxSize: 100 }),
6766
};
6867

0 commit comments

Comments
 (0)