perf(vite): rework endsWith to direct index#24746
Conversation
|
|
|
|
||
| // 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.
@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.
yes, I think it's possible it will end with whitespace...
There was a problem hiding this comment.
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.
you could iterate backwards in a while loop... 🤔
There was a problem hiding this comment.
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.
|
@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