-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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(vite): rework endsWith to direct index #24746
Conversation
Run & review this pull request in StackBlitz Codeflow. |
@@ -116,7 +116,8 @@ export const composableKeysPlugin = createUnplugin((options: ComposableKeysOptio | |||
} | |||
|
|||
// TODO: Optimize me (https://github.com/nuxt/framework/pull/8529) | |||
const endsWithComma = code.slice(codeIndex + (node as any).start, codeIndex + (node as any).end - 1).trim().endsWith(',') | |||
const newCode = code.slice(codeIndex + (node as any).start, codeIndex + (node as any).end - 1).trim() |
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.
@danielroe this entire slice/trim operation, is it even needed? Can we not just do this?
code[codeIndex + (node as any).end - 1]===','
I saw that pi left a comment there, is there a possibility codeIndex + (node as any).end - 1
would point to a whitespace?
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.
yes, I think it's possible it will end with whitespace...
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.
Hmm, I'll try to run some tests with this. There's gotta be a way to make it work without it..
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.
you could iterate backwards in a while loop... π€
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.
That could be possible, it will at the very least reduce the amount of characters "digested" by trim I suppose which runs both ways (will make some benchmarks for this), but I wonder if there's a way to make it even faster, like if the expression itself will not contain whitespace, in which case last index could be used directly.
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.
@danielroe would you like me to also fix this in tests? Or keep this in packages only? |
We can ignore tests. π |
π |
I'm getting some odd results and even odder deviations in my analysis. We should count on offline benchmarks, right? Ones made on a PC rather than on the browser? |
Benchmarks should be on node and bun, for changes to kit/schema/nuxt build. I would take perf optimisations with an extreme grain of salt as runtimes frequently optimise newer features in future... In general avoiding sweeping statements. Ideally reproducible benchmarks should be provided - might be worth using something like codspeed, and adding a comment to remove when things change. |
I had a feeling it was just the online benchmarks faking because on node it is marginally faster.
I can do that, but considering endsWith was implemented in ECMA6 and hasn't been optimized since I can say pretty confidently this isn't likely to change anytime soon. |
Well, a little underwhelming that there was only one thing to change that actually makes any sort of difference, but I guess this happens =) |
π Linked issue
nuxt/framework#8529
β Type of change
π Description
Similar to #24744, endsWith also has a performance downside when checking single characters. This reworks it to direct index in strings, which inccreases performance by ~40%.
Benchmark results:
And yes, here too we can make a tradeoff of partial readability for performance if needed.
π Checklist