-
-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Performance improvment: Add format "raw" for "string" type (skip escaping) #685
Conversation
Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
fix lint Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
Can you add some docs? |
@@ -113,7 +113,9 @@ module.exports = class Serializer { | |||
} | |||
} | |||
|
|||
if (str.length < 42) { | |||
if (format === 'raw') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would create a different serializer method instead of adding the check here, because you know the if condition at the compile time and there is no need to to it in a serialization time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mhh eg. asStringRaw(str)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeap
@@ -4,6 +4,22 @@ const t = require('tap') | |||
const test = t.test | |||
const build = require('..') | |||
|
|||
test('serialize short string raw', (t) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add some test cases where it doesn't escape chars
@@ -113,7 +113,9 @@ module.exports = class Serializer { | |||
} | |||
} | |||
|
|||
if (str.length < 42) { | |||
if (format === 'raw') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use a different name than raw
to emphasize that is format has a very dangerous potential security issue. Raw format would not escape a double quote char which makes it very easy to inject something into the data. You can use it only if you 200% sure in your data. I would call it something like unsafe
.
@mcollina WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for unsafe ...
Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
Done. 4f30811 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
@@ -284,7 +284,7 @@ function buildExtraObjectPropertiesSerializer (context, location, addComma) { | |||
code += ` | |||
if (/${propertyKey.replace(/\\*\//g, '\\/')}/.test(key)) { | |||
${addComma} | |||
json += serializer.asString(key) + ':' | |||
json += serializer.asString(key,null) + ':' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why pass null? It’s type is object while the parameter type is string — to me that gives me the impression that an object argument is expected there
What’s wrong with leaving it as undefined?
json += serializer.asString(key,null) + ':' | |
json += serializer.asString(key) + ':' |
superseded by #686 |
JSON with a lot of strings take time during escaping.
This PR introduce "raw" string format (no need to escape).
benchmark (compare classic bench VS same bench with format raw)
raw string seem x45 faster