From f307c11fb170a79f0c1703aee52797a8ed27af57 Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Wed, 12 Apr 2023 10:16:24 +0700 Subject: [PATCH] feat: only compute path on first getPath() call --- src/index.ts | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/index.ts b/src/index.ts index f368dbe5..8a5513af 100644 --- a/src/index.ts +++ b/src/index.ts @@ -497,6 +497,7 @@ class DefaultMultiaddr implements Multiaddr { #string?: string #tuples?: Tuple[] #stringTuples?: StringTuple[] + #path?: string | null [symbol]: boolean = true @@ -683,23 +684,27 @@ class DefaultMultiaddr implements Multiaddr { } getPath (): string | null { - let path = null - try { - path = this.stringTuples().filter((tuple) => { - const proto = getProtocol(tuple[0]) - if (proto.path === true) { - return true + // on initialization, this.#path is undefined + // after the first call, it is either a string or null + if (this.#path === undefined) { + try { + this.#path = this.stringTuples().filter((tuple) => { + const proto = getProtocol(tuple[0]) + if (proto.path === true) { + return true + } + return false + })[0][1] + + if (this.#path == null) { + this.#path = null } - return false - })[0][1] - - if (path == null) { - path = null + } catch { + this.#path = null } - } catch { - path = null } - return path + + return this.#path } equals (addr: { bytes: Uint8Array }): boolean {