Skip to content
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

[fix] Improve performance by using trimRight() instead of regex replace #7706

Merged
merged 1 commit into from Aug 23, 2022

Conversation

ota-meshi
Copy link
Member

Before submitting the PR, please make sure you do the following

  • 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
  • Prefix your PR title with [feat], [fix], [chore], or [docs].
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with npm test and lint the project with npm run lint

close #7675

This PR uses trimRight() instead of replace(/\s+$/, '') to improve parsing performance.

See the #7675 (comment) 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>`);
	});
});

@benmccann
Copy link
Member

benmccann commented Jul 28, 2022

I wonder if the benchmark would give different results after @ivanhofer's change: #7716

@ota-meshi
Copy link
Member Author

I don't think the #7716 change will change the benchmark results. This fix is related to backtracking.
See #7675 (comment).

@ivanhofer
Copy link
Contributor

ivanhofer commented Aug 8, 2022

@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?

@ota-meshi
Copy link
Member Author

/^\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.

@shirotech
Copy link

Seems to have ticked all the boxes, any chance this can be merged soon?

@benmccann benmccann added the perf label Aug 23, 2022
@benmccann benmccann merged commit 8d26b4a into sveltejs:master Aug 23, 2022
@ota-meshi ota-meshi deleted the use-trim-right branch August 23, 2022 02:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Slow parsing when blanks are continuous
5 participants