Skip to content
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

perf: use regexp in string escaping #553

Merged
merged 2 commits into from
Oct 27, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/serializer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict'

// eslint-disable-next-line
const STR_ESCAPE = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?:[^\ud800-\udbff]|^)[\udc00-\udfff]/
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved

module.exports = class Serializer {
constructor (options = {}) {
switch (options.rounding) {
Expand Down Expand Up @@ -94,6 +97,10 @@ module.exports = class Serializer {
str = str.toString()
}

if (!STR_ESCAPE.test(str)) {
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please include a comment above this line telling why it's needed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new check does not execute the str.length < 42 optimization. Is this wanted or could we go even faster?

return quotes + str + quotes
}

if (str.length < 42) {
return this.asStringSmall(str)
} else {
Expand Down