faster serialization for deserialized data
This commit is contained in:
parent
39431ee8a5
commit
d8c93b40be
|
@ -10,6 +10,8 @@ const SerializerMiddleware = require("./SerializerMiddleware");
|
|||
/** @typedef {import("./types").BufferSerializableType} BufferSerializableType */
|
||||
/** @typedef {import("./types").PrimitiveSerializableType} PrimitiveSerializableType */
|
||||
|
||||
const SERIALIZED_INFO = Symbol("serialized info");
|
||||
|
||||
/*
|
||||
Format:
|
||||
|
||||
|
@ -86,6 +88,8 @@ const identifyNumber = n => {
|
|||
*/
|
||||
class BinaryMiddleware extends SerializerMiddleware {
|
||||
_handleFunctionSerialization(fn, context) {
|
||||
const serializedInfo = fn[SERIALIZED_INFO];
|
||||
if (serializedInfo) return serializedInfo;
|
||||
return memorize(() => {
|
||||
const r = fn();
|
||||
if (r instanceof Promise)
|
||||
|
@ -96,12 +100,14 @@ class BinaryMiddleware extends SerializerMiddleware {
|
|||
}
|
||||
|
||||
_handleFunctionDeserialization(fn, context) {
|
||||
return memorize(() => {
|
||||
const result = memorize(() => {
|
||||
const r = fn();
|
||||
if (r instanceof Promise)
|
||||
return r.then(data => this.deserialize(data, context));
|
||||
return this.deserialize(r, context);
|
||||
});
|
||||
result[SERIALIZED_INFO] = fn;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,6 +18,8 @@ const SetObjectSerializer = require("./SetObjectSerializer");
|
|||
|
||||
/** @typedef {new (...params: any[]) => any} Constructor */
|
||||
|
||||
const SERIALIZED_INFO = Symbol("serialized info");
|
||||
|
||||
/*
|
||||
|
||||
Format:
|
||||
|
@ -224,6 +226,8 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|||
}
|
||||
|
||||
_handleFunctionSerialization(fn, context) {
|
||||
const serializedInfo = fn[SERIALIZED_INFO];
|
||||
if (serializedInfo) return serializedInfo;
|
||||
return memorize(() => {
|
||||
const r = fn();
|
||||
|
||||
|
@ -235,7 +239,7 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|||
}
|
||||
|
||||
_handleFunctionDeserialization(fn, context) {
|
||||
return memorize(() => {
|
||||
const result = memorize(() => {
|
||||
const r = fn();
|
||||
|
||||
if (r instanceof Promise)
|
||||
|
@ -243,6 +247,8 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|||
|
||||
return this.deserialize(r, context)[0];
|
||||
});
|
||||
result[SERIALIZED_INFO] = fn;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue