-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve uint8array serializing perf (#13)
Signed-off-by: Marcos Candeia <[email protected]>
- Loading branch information
Showing
3 changed files
with
34 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export interface SerializedUint8Array { | ||
__uint8array: number[]; | ||
} | ||
|
||
const isSerializedUint8Array = ( | ||
s: unknown | SerializedUint8Array, | ||
): s is SerializedUint8Array => { | ||
return typeof s === "object" && | ||
(s as SerializedUint8Array)?.__uint8array instanceof Array; | ||
}; | ||
export const serializeUint8Array = (_: string, value: unknown) => { | ||
if (value instanceof Uint8Array) { | ||
return { | ||
__uint8array: Array.from(value), // Mark arrays that should be Uint8Array | ||
}; | ||
} | ||
return value; | ||
}; | ||
|
||
export const deserializeUint8Array = ( | ||
_: string, | ||
value: unknown | SerializedUint8Array, | ||
) => { | ||
if (isSerializedUint8Array(value)) { | ||
return new Uint8Array(value.__uint8array); | ||
} | ||
return value; | ||
}; |