perf(common): faster logs by caching intl.datetimeformat #10823
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
This is the profiler information when we call
#log
method 10.000 times:As you can see,
getTimestamp
consumes most of the execution time, which doesn't make sense, I did some CPU profiling and I found this:The
#getTimestamp
method took 1ms, in another test that I did, this functionWhen we run again on nodejs16 or nodejs19:
The
#getTimestamp
method took 13ms (I think I found a regression on NodeJS).Also, decorators like
WrapBuffer
tends to not be optimized by V8, so this could be another point of optimization.Issue Number: N/A
What is the new behavior?
So, to solve the issue with
#getTimestamp
, I started usingIntl.DateTimeFormat
API,and for the decorator
WrapBuffer
, I just refactor to use a function.The final profiler information became:
And the CPU profiling now looks like:
And the time to perform the log looks consistent between NodeJS environments.
Finally, about the performance improvements, this is the result running on NodeJS 16:
An improvement of 8x in the performance when logging.
I didn't make any breaking changes in the logs, but if we change from
Intl.DateTimeFormat
API to justnew Date().toISOString()
, the benchmark will look like:So let me know if worth to introduce some breaking changes just to have better performance when logging.
To reproduce all those tests, see this gist: https://gist.github.com/H4ad/da838dfc33b2060504ca980644d804b5
Does this PR introduce a breaking change?
Other information