Skip to content

Commit 890dab0

Browse files
authored
chore: replace calls of the slow superstruct "is" with faster regex (#270)
<!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? Are there any issues or other links reviewers should consult to understand this pull request better? For instance: * Fixes #12345 * See: #67890 --> The current use of the `is` function from the `superstruct` package is drastically slowing down caip type checks. This replaces those function calls with much faster regex checks.
1 parent 40ed2bd commit 890dab0

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/caip-types.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Infer } from '@metamask/superstruct';
2-
import { is } from '@metamask/superstruct';
32

43
import { definePattern } from './superstruct';
54

@@ -164,7 +163,7 @@ export type KnownCaipNamespacedChainId<
164163
* @returns Whether the value is a {@link CaipChainId}.
165164
*/
166165
export function isCaipChainId(value: unknown): value is CaipChainId {
167-
return is(value, CaipChainIdStruct);
166+
return typeof value === 'string' && CAIP_CHAIN_ID_REGEX.test(value);
168167
}
169168

170169
/**
@@ -174,7 +173,7 @@ export function isCaipChainId(value: unknown): value is CaipChainId {
174173
* @returns Whether the value is a {@link CaipNamespace}.
175174
*/
176175
export function isCaipNamespace(value: unknown): value is CaipNamespace {
177-
return is(value, CaipNamespaceStruct);
176+
return typeof value === 'string' && CAIP_NAMESPACE_REGEX.test(value);
178177
}
179178

180179
/**
@@ -184,7 +183,7 @@ export function isCaipNamespace(value: unknown): value is CaipNamespace {
184183
* @returns Whether the value is a {@link CaipReference}.
185184
*/
186185
export function isCaipReference(value: unknown): value is CaipReference {
187-
return is(value, CaipReferenceStruct);
186+
return typeof value === 'string' && CAIP_REFERENCE_REGEX.test(value);
188187
}
189188

190189
/**
@@ -194,7 +193,7 @@ export function isCaipReference(value: unknown): value is CaipReference {
194193
* @returns Whether the value is a {@link CaipAccountId}.
195194
*/
196195
export function isCaipAccountId(value: unknown): value is CaipAccountId {
197-
return is(value, CaipAccountIdStruct);
196+
return typeof value === 'string' && CAIP_ACCOUNT_ID_REGEX.test(value);
198197
}
199198

200199
/**
@@ -206,7 +205,7 @@ export function isCaipAccountId(value: unknown): value is CaipAccountId {
206205
export function isCaipAccountAddress(
207206
value: unknown,
208207
): value is CaipAccountAddress {
209-
return is(value, CaipAccountAddressStruct);
208+
return typeof value === 'string' && CAIP_ACCOUNT_ADDRESS_REGEX.test(value);
210209
}
211210

212211
/**
@@ -218,7 +217,7 @@ export function isCaipAccountAddress(
218217
export function isCaipAssetNamespace(
219218
value: unknown,
220219
): value is CaipAssetNamespace {
221-
return is(value, CaipAssetNamespaceStruct);
220+
return typeof value === 'string' && CAIP_ASSET_NAMESPACE_REGEX.test(value);
222221
}
223222

224223
/**
@@ -230,7 +229,7 @@ export function isCaipAssetNamespace(
230229
export function isCaipAssetReference(
231230
value: unknown,
232231
): value is CaipAssetReference {
233-
return is(value, CaipAssetReferenceStruct);
232+
return typeof value === 'string' && CAIP_ASSET_REFERENCE_REGEX.test(value);
234233
}
235234

236235
/**
@@ -240,7 +239,7 @@ export function isCaipAssetReference(
240239
* @returns Whether the value is a {@link CaipTokenId}.
241240
*/
242241
export function isCaipTokenId(value: unknown): value is CaipTokenId {
243-
return is(value, CaipTokenIdStruct);
242+
return typeof value === 'string' && CAIP_TOKEN_ID_REGEX.test(value);
244243
}
245244

246245
/**
@@ -250,7 +249,7 @@ export function isCaipTokenId(value: unknown): value is CaipTokenId {
250249
* @returns Whether the value is a {@link CaipAssetType}.
251250
*/
252251
export function isCaipAssetType(value: unknown): value is CaipAssetType {
253-
return is(value, CaipAssetTypeStruct);
252+
return typeof value === 'string' && CAIP_ASSET_TYPE_REGEX.test(value);
254253
}
255254

256255
/**
@@ -260,7 +259,7 @@ export function isCaipAssetType(value: unknown): value is CaipAssetType {
260259
* @returns Whether the value is a {@link CaipAssetId}.
261260
*/
262261
export function isCaipAssetId(value: unknown): value is CaipAssetId {
263-
return is(value, CaipAssetIdStruct);
262+
return typeof value === 'string' && CAIP_ASSET_ID_REGEX.test(value);
264263
}
265264

266265
/**

0 commit comments

Comments
 (0)