Skip to content

Commit bebb0a7

Browse files
feat: cache b58 id
1 parent 5d6a962 commit bebb0a7

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/index.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ class PeerId {
1717
assert(privKey.public.bytes.equals(pubKey.bytes), 'inconsistent arguments')
1818
}
1919

20-
this.id = id
20+
this._id = id
21+
this._idB58String = ''
2122
this._privKey = privKey
2223
this._pubKey = pubKey
2324
}
2425

26+
get id () {
27+
return this._id
28+
}
29+
2530
get privKey () {
2631
return this._privKey
2732
}
@@ -61,7 +66,7 @@ class PeerId {
6166
// of go-ipfs for its config file
6267
toJSON () {
6368
return {
64-
id: mh.toB58String(this.id),
69+
id: this.toB58String(),
6570
privKey: toB64Opt(this.marshalPrivKey()),
6671
pubKey: toB64Opt(this.marshalPubKey())
6772
}
@@ -77,7 +82,11 @@ class PeerId {
7782
}
7883

7984
toB58String () {
80-
return mh.toB58String(this.id)
85+
if (!this._idB58String) {
86+
this._idB58String = mh.toB58String(this.id)
87+
}
88+
89+
return this._idB58String
8190
}
8291
}
8392

0 commit comments

Comments
 (0)