svelte icon indicating copy to clipboard operation
svelte copied to clipboard

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

Open ota-meshi opened this issue 2 years ago • 4 comments

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 with npm 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>`);
	});
});

ota-meshi avatar Jul 21 '22 10:07 ota-meshi

I wonder if the benchmark would give different results after @ivanhofer's change: https://github.com/sveltejs/svelte/pull/7716

benmccann avatar Jul 28 '22 15:07 benmccann

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 avatar Jul 28 '22 16:07 ota-meshi

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

ivanhofer avatar Aug 08 '22 05:08 ivanhofer

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

ota-meshi avatar Aug 08 '22 15:08 ota-meshi

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

shirotech avatar Aug 23 '22 00:08 shirotech