-
Notifications
You must be signed in to change notification settings - Fork 30k
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
util: escaping object keys in util.inspect() #16986
Conversation
Hello @buji1993 and welcome. Thank you very much for the contribution 🥇 P.S. If you have any question you can also feel free to contact me directly (here, on IRC, or anywhere else). |
test/parallel/test-util-inspect.js
Outdated
|
||
assert.strictEqual( | ||
util.inspect(w), | ||
'{ \'\\\': 1, \'\\\\\': 2, \'\\\\\\\': 3, \'\\\\\\\\\': 4 }' | ||
'{ \'\\\\\': 2, \'\\\\\\\\\': 4, \'\\\\\\\\\\\\\': 6, ' + | ||
'\'\\\\\\\\\\\\\\\\\': 8 }' | ||
); | ||
assert.strictEqual( |
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.
Suggestion, could you add a test with \r
and \n
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.
thanks for your suggestion, I will add it later
Is this semver-major? It seems like a bugfix, but it changes outputs. |
I'd argue for bug fix, but once we land, we might want to wait a while before backporting to LTS |
if (str.length < 5000 && !keyEscapeSequencesRegExp.test(str)) | ||
return `'${str}'`; | ||
if (str.length > 100) | ||
return `'${str.replace(keyEscapeSequencesReplacer, escapeFn)}'`; |
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.
Not specific to this PR but to the code:
@bmeurer in my tests RegExp.p.test
performs better than String.p.replace
. Should those not be on par in case no match is found? And with small strings it is (last tested with 6.1) still better to use String.p.charCodeAt
instead of the RegExp. Is there any chance to improve the RegExp so it would match the performance of charCodeAt
?
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.
@bmeurer in my tests RegExp.p.test performs better than String.p.replace. Should those not be on par in case no match is found?
test
is a much simpler operation than @@replace
(see the spec at [0] and [1]). It's fairly easy to implement test
efficiently; but in my experience @@replace
is another story, see V8's @@replace
dispatch logic at [2] to feel our pain.
In this particular case, we seem to reach the ReplaceGlobalCallableFastPath
. There's definitely a couple of things we could do to improve here, like remove one (or both) runtime calls. Could you open a bug at crbug.com/v8/new?
[0] https://tc39.github.io/ecma262/#sec-regexp.prototype.test
[1] https://tc39.github.io/ecma262/#sec-regexp.prototype-@@replace
[2] https://cs.chromium.org/chromium/src/v8/src/builtins/builtins-regexp-gen.cc?l=3058&rcl=2eea37273b30caa5cedf3a5c8d656860bc60320b
And with small strings it is (last tested with 6.1) still better to use String.p.charCodeAt instead of the RegExp. Is there any chance to improve the RegExp so it would match the performance of charCodeAt?
I assume you mean charCodeAt
vs. test
? It'll be hard to beat, since charCodeAt
is completely inlined by the optimizing compiler. I suppose one possible step we could take would be to eliminate the call overhead from RegExp builtins, but I wouldn't expect performance to improve by alot.
Could you share the benchmark you used to measure this?
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.
Could you open a bug at crbug.com/v8/new?
Went ahead and created https://crbug.com/v8/7081.
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
Seems like the original reason for this was another bug (nodejs/node-v0.x-archive#6835) that was fixed with a not ideal solution.
@refack thanks, it's so nice! I will contact you if I need help |
@schuay is probably a good person to answer this question. |
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.
PRs that fix a bug by removing code are my favourite. 🥇
Landed in 71ee0d9, thanks for the PR! ✨ |
as this may have unexpected breakages we are waiting a bit before landing on lts |
@MylesBorins we now got this in 9 for a while without any complains I think we can go ahead and backport this. I added the backport requested label but it might actually apply cleanly? |
@BridgeAR the edit: I've landed in" |
I've opted to not land on v6.x. Please feel free to change labels and open backport |
Fixes: #16979
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
lib