svelte
svelte copied to clipboard
[fix] Improve performance by using trimRight() instead of regex replace
Before submitting the PR, please make sure you do the following
- [x] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- [x] Prefix your PR title with
[feat]
,[fix]
,[chore]
, or[docs]
. - [x] This message body should clearly illustrate what problems it solves.
- [ ] Ideally, include a test that fails without this PR but passes with it.
Tests
- [x] Run the tests with
npm test
and lint the project withnpm run lint
close #7675
This PR uses trimRight()
instead of replace(/\s+$/, '')
to improve parsing performance.
See the https://github.com/sveltejs/svelte/issues/7675#issuecomment-1179995722 for issues with the original regex.
If you know how to add a test to check performance, please let me know. I ran the following test locally and I checked that it would improve performance.
import { parse } from '../../compiler.js';
describe('improve performance', () => {
it('parse', () => {
parse(`<div>${' '.repeat(1000000)}</div>`);
});
});
I wonder if the benchmark would give different results after @ivanhofer's change: https://github.com/sveltejs/svelte/pull/7716
I don't think the #7716 change will change the benchmark results. This fix is related to backtracking. See https://github.com/sveltejs/svelte/issues/7675#issuecomment-1179995722.
@ota-meshi what about using trimLeft()
here: https://github.com/ota-meshi/svelte/blob/use-trim-right/src/compiler/compile/render_ssr/index.ts#L231?
/^\s+/
don't backtrack, so we don't need to fix that. However, I think it makes sense to use trimLeft()
for the purpose of unifying code style. I think it depends on what the team prefers.
Seems to have ticked all the boxes, any chance this can be merged soon?