-
-
Notifications
You must be signed in to change notification settings - Fork 9
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: Improve performance #69
Conversation
We could replace codes Object with a codes-Map and after warning we could actually delete the warning from the codes Map |
delete warning after emitting
I resolved that remark. Open a new remark if you still disagree. |
I added label semver-major because the emitted-property gets changed from a Map to Set, breaking the api contract. |
Please don't mark other people's reviews resolved unless they indicate it is resolved. |
Could you please describe what you have in mind? I dont expect that you will take over this PR. But you have to give me more than a "I think this PR has shown that the current implementation can remove the codes plain JavaScript object and utilize a single Map for emitted", so that I could have the slightest chance go implement what you have in mind. Imho the double structure should have the best performance. So i am in doubt that having a single structure will have benefits other than a little bit less memory |
When we add a new code, we add it to the |
Using a single object would be something like below. const codes = new Map()
function create (name, code, message) {
// ...
return { name, code, message: formatted, emitted: false }
}
function emit (code, a, b, c) {
const info = codes.get(code)
if (info === undefined) throw new Error(`The code '${code}' does not exist`)
if (info.emitted === true) return
// ...
info.emitted = true
} |
I benched a little. It seems like that using Objects in both cases boosts the performance significantly. As if v8 optimizes it away. uzlopak@Battlestation:~/workspace/process-warning$ npm run benchmark
> process-warning@2.0.0 benchmark
> node benchmark/benchmark.js
warn x 1,250,369,939 ops/sec ±0.17% (98 runs sampled)
(node:109564) [FST_ERROR_CODE] FastifyWarning: message
(Use `node --trace-warnings ...` to show where the warning was created) |
Same benchmark on master uzlopak@Battlestation:~/workspace/process-warning$ node benchmark/benchmark.js
warn x 155,841,579 ops/sec ±0.24% (96 runs sampled)
(node:114029) [FST_ERROR_CODE] FastifyWarning: message
(Use `node --trace-warnings ...` to show where the warning was created) |
Would you please give a statement? If my changes are negligible than please close this PR. |
Is this sitting in a hot path of some kind? How did you reach the conclusion to optimize this? The change looks ok to me. |
better benchmark
node16: uzlopak@Battlestation:~/workspace/process-warning$ node benchmark/benchmark.js
warn x 39,713,659 ops/sec ±0.98% (97 runs sampled)
(node:119186) [FST_ERROR_CODE_1] FastifyWarning: message
(Use `node --trace-warnings ...` to show where the warning was created)
(node:119186) [FST_ERROR_CODE_3] FastifyWarning: message after: uzlopak@Battlestation:~/workspace/process-warning$ node benchmark/benchmark.js
warn x 77,635,234 ops/sec ±0.40% (98 runs sampled)
(node:119394) [FST_ERROR_CODE_1] FastifyWarning: message
(Use `node --trace-warnings ...` to show where the warning was created)
(node:119394) [FST_ERROR_CODE_3] FastifyWarning: message In node14 the I even get 105m ops/s. Really strange. @mcollina |
return codes[code] | ||
} | ||
|
||
function emit (code, a, b, c) { | ||
if (emitted[code] === true) return |
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.
The main performance gain is the return
line moved above the check of codes
exist.
Since the function here is simple enough, one more check means double of time.
For what it's worth, the exported |
Good point. I will modify it. |
Closing in favor of #70 |
I assume that a Set is more performant than a Map with checking the value.
Checklist
npm run test
andnpm run benchmark
and the Code of conduct