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(core): optimize reflector #10112
perf(core): optimize reflector #10112
Conversation
@@ -86,6 +86,6 @@ export class Reflector { | |||
metadataKey: TKey, | |||
targets: (Type<any> | Function)[], | |||
): TResult { | |||
return this.getAll(metadataKey, targets).find(item => item !== undefined); | |||
return targets.find(target => this.get(metadataKey, target) !== undefined); |
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 this instead of the this.getAll()
? Just for an early stop if a value is found?
fc2c93b
to
a3b8256
Compare
for (const target of targets) { | ||
const result = this.get(metadataKey, target); | ||
if (result !== undefined) { | ||
return result; | ||
} | ||
} | ||
return undefined; |
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.
If you're going to refactor this to a raw for loop, we should do the same to the getAll
for squeezing as much performance out as we can.
Side note: do you have any benchmarks for how much of an improvement this is? Just curious if the improvement is going to be worth the readability
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 think raw for loop is not the mainly reason. Just break the loop earlier to increase the performance.
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.
Seems not increase too much. This is a choice between a little performance and readability. :P
function get(num) {
for(let i = 0; i < 100; i++) {
num++
}
return num
}
function test1() {
const start = process.hrtime.bigint();
for(let i = 0; i< 10000; i++) {
const targets = [100, 1000, 10000]
targets.map(target=>get(target)).find(item => item !== undefined)
}
console.log(process.hrtime.bigint()-start);
}
function test2() {
const start = process.hrtime.bigint();
for(let i = 0; i< 10000; i++) {
const targets = [100, 1000, 10000]
for(const target of targets) {
const res = get(target)
if(res !== undefined) {
break;
}
}
}
console.log(process.hrtime.bigint()-start);
}
function test3() {
const start = process.hrtime.bigint();
for(let i = 0; i< 10000; i++) {
const targets = [100, 1000, 10000]
for(const target of targets) {
const res = get(target)
if(res !== undefined) {
// break;
}
}
}
console.log(process.hrtime.bigint()-start);
}
test1() // 119022900n
test2() // 4902500n
test3() // 7564600n
Pull Request Test Coverage Report for Build f3608146-d290-4a7e-a1ce-5fbbf02b5f37
💛 - Coveralls |
lgtm |
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?
Issue Number: N/A
What is the new behavior?
Does this PR introduce a breaking change?
Other information