From 76b7b6d90b738fe9c10be050b231749419b61198 Mon Sep 17 00:00:00 2001 From: francesco Date: Wed, 11 Dec 2024 11:47:44 +0100 Subject: [PATCH 1/3] perf: direct access to serialize method Signed-off-by: francesco --- index.js | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 2390c28a..57d57d38 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,21 @@ const SINGLE_TICK = /'/g let largeArraySize = 2e4 let largeArrayMechanism = 'default' +const serializerFns = ` +let { + asString, + asInteger, + asNumber, + asBoolean, + asDateTime, + asDate, + asTime, +} = serializer + +asNumber = asNumber.bind(serializer) + +` + const validRoundingMethods = [ 'floor', 'ceil', @@ -141,6 +156,7 @@ function build (schema, options) { const code = buildValue(context, location, 'input') let contextFunctionCode = ` + ${serializerFns} const JSON_STR_BEGIN_OBJECT = '{' const JSON_STR_END_OBJECT = '}' const JSON_STR_BEGIN_ARRAY = '[' @@ -292,7 +308,7 @@ function buildExtraObjectPropertiesSerializer (context, location, addComma) { code += ` if (/${propertyKey.replace(/\\*\//g, '\\/')}/.test(key)) { ${addComma} - json += serializer.asString(key) + JSON_STR_COLONS + json += asString(key) + JSON_STR_COLONS ${buildValue(context, propertyLocation, 'value')} continue } @@ -307,13 +323,13 @@ function buildExtraObjectPropertiesSerializer (context, location, addComma) { if (additionalPropertiesSchema === true) { code += ` ${addComma} - json += serializer.asString(key) + JSON_STR_COLONS + JSON.stringify(value) + json += asString(key) + JSON_STR_COLONS + JSON.stringify(value) ` } else { const propertyLocation = location.getPropertyLocation('additionalProperties') code += ` ${addComma} - json += serializer.asString(key) + JSON_STR_COLONS + json += asString(key) + JSON_STR_COLONS ${buildValue(context, propertyLocation, 'value')} ` } @@ -729,13 +745,13 @@ function buildSingleTypeSerializer (context, location, input) { return 'json += JSON_STR_NULL' case 'string': { if (schema.format === 'date-time') { - return `json += serializer.asDateTime(${input})` + return `json += asDateTime(${input})` } else if (schema.format === 'date') { - return `json += serializer.asDate(${input})` + return `json += asDate(${input})` } else if (schema.format === 'time') { - return `json += serializer.asTime(${input})` + return `json += asTime(${input})` } else if (schema.format === 'unsafe') { - return `json += serializer.asUnsafeString(${input})` + return `json += asUnsafeString(${input})` } else { return ` if (typeof ${input} !== 'string') { @@ -744,22 +760,22 @@ function buildSingleTypeSerializer (context, location, input) { } else if (${input} instanceof Date) { json += JSON_STR_QUOTE + ${input}.toISOString() + JSON_STR_QUOTE } else if (${input} instanceof RegExp) { - json += serializer.asString(${input}.source) + json += asString(${input}.source) } else { - json += serializer.asString(${input}.toString()) + json += asString(${input}.toString()) } } else { - json += serializer.asString(${input}) + json += asString(${input}) } ` } } case 'integer': - return `json += serializer.asInteger(${input})` + return `json += asInteger(${input})` case 'number': - return `json += serializer.asNumber(${input})` + return `json += asNumber(${input})` case 'boolean': - return `json += serializer.asBoolean(${input})` + return `json += asBoolean(${input})` case 'object': { const funcName = buildObject(context, location) return `json += ${funcName}(${input})` From ee31dab28ed9aac4d30af5b58c48e90feb4ac76e Mon Sep 17 00:00:00 2001 From: francesco Date: Wed, 11 Dec 2024 11:49:42 +0100 Subject: [PATCH 2/3] fix: bind on asInteger Signed-off-by: francesco --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 57d57d38..27e12c2e 100644 --- a/index.js +++ b/index.js @@ -26,7 +26,7 @@ let { asTime, } = serializer -asNumber = asNumber.bind(serializer) +asInteger = asInteger.bind(serializer) ` From f442d01d9fbc152e03c4a26515181d4857788146 Mon Sep 17 00:00:00 2001 From: francesco Date: Wed, 11 Dec 2024 11:51:29 +0100 Subject: [PATCH 3/3] fix: missing asUnsafeString Signed-off-by: francesco --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index 27e12c2e..42863552 100644 --- a/index.js +++ b/index.js @@ -24,6 +24,7 @@ let { asDateTime, asDate, asTime, + asUnsafeString } = serializer asInteger = asInteger.bind(serializer)