perf(transformer/class-properties): replace recursion with loop#7652
Conversation
Your org has enabled the Graphite merge queue for merging into mainAdd the label “0-merge” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge. Or use the label “hotfix” to add to the merge queue as a hot fix. You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link. |
CodSpeed Performance ReportMerging #7652 will not alter performanceComparing Summary
|
camc314
left a comment
There was a problem hiding this comment.
For my own self developement, are there any good resources on why this is faster? 🙂
I don't have a go-to article or anything on this. But the general principle is that a function recursively calling itself increases the call stack. That is:
Clever compilers will convert recursive functions to loops anyway ("tail call optimization"), but often the function has to be quite simple, or written in a certain way, for the compiler to be able to see that optimization is possible. So maybe compiler would have done this itself anyway here, maybe not. I believe that in principle any recursive algorithm can be rewritten as a loop, but with complex algorithms often the way you have to write the code to achieve that makes it pretty much unreadable. But on a fairly simple functions like this where it doesn't make the code completely incomprehensible, personally I think it's worthwhile doing. Here's a rather silly example showing the perf impact, from a conversation I was having with someone the other week about exactly this. Flattening an array of arrays in JS: https://x.com/onlyspaceghost/status/1859398231915925988 The loopy version is 1310x faster! If you're interested in learning more, search for "loop vs recursion" or "tail call optimization". |
|
🙏 thank you |
Merge activity
|
Follow-on after #7575. Small optimization. Replace recursive function calls with a loop.
8fd9090 to
6c82589
Compare

Follow-on after #7575. Small optimization. Replace recursive function calls with a loop.