Skip to content

Commit

Permalink
perf(compiler-sfc): remove magic-string trim on script
Browse files Browse the repository at this point in the history
magic-string's trim method uses a regex check for aborting which turns
out to be extremely expensive - it cna take up to 10% time in total SFC
compilation! The usage here is purely aesthetic so simply removing it
for a big perf gain is well worth it.
  • Loading branch information
yyx990803 committed Nov 25, 2023
1 parent 2073236 commit e8e3ec6
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ return { a }
`;
exports[`SFC analyze <script> bindings > auto name inference > do not overwrite manual name (call) 1`] = `
"import { defineComponent } from 'vue'
"
import { defineComponent } from 'vue'
const __default__ = defineComponent({
name: 'Baz'
})
Expand All @@ -30,7 +31,8 @@ return { a, defineComponent }
`;
exports[`SFC analyze <script> bindings > auto name inference > do not overwrite manual name (object) 1`] = `
"const __default__ = {
"
const __default__ = {
name: 'Baz'
}
Expand All @@ -45,7 +47,8 @@ return { a }
`;
exports[`SFC compile <script setup> > <script> and <script setup> co-usage > export call expression as default 1`] = `
"function fn() {
"
function fn() {
return \\"hello, world\\";
}
const __default__ = fn();
Expand All @@ -63,7 +66,8 @@ return { fn }
`;
exports[`SFC compile <script setup> > <script> and <script setup> co-usage > keep original semi style 1`] = `
"export default {
"
export default {
props: ['item'],
emits: ['change'],
setup(__props, { expose: __expose, emit: __emit }) {
Expand Down Expand Up @@ -535,7 +539,8 @@ return { get a() { return a }, set a(v) { a = v } }
`;
exports[`SFC compile <script setup> > async/await detection > should ignore await inside functions 1`] = `
"export default {
"
export default {
setup(__props, { expose: __expose }) {
__expose();
async function foo() { await bar }
Expand All @@ -546,7 +551,8 @@ return { foo }
`;
exports[`SFC compile <script setup> > async/await detection > should ignore await inside functions 2`] = `
"export default {
"
export default {
setup(__props, { expose: __expose }) {
__expose();
const foo = async () => { await bar }
Expand All @@ -557,7 +563,8 @@ return { foo }
`;
exports[`SFC compile <script setup> > async/await detection > should ignore await inside functions 3`] = `
"export default {
"
export default {
setup(__props, { expose: __expose }) {
__expose();
const obj = { async method() { await bar }}
Expand All @@ -568,7 +575,8 @@ return { obj }
`;
exports[`SFC compile <script setup> > async/await detection > should ignore await inside functions 4`] = `
"export default {
"
export default {
setup(__props, { expose: __expose }) {
__expose();
const cls = class Foo { async method() { await bar }}
Expand Down Expand Up @@ -618,7 +626,8 @@ return { a }
`;
exports[`SFC compile <script setup> > binding analysis for destructure 1`] = `
"export default {
"
export default {
setup(__props, { expose: __expose }) {
__expose();
Expand Down Expand Up @@ -838,7 +847,9 @@ return { msg, useCssVars, ref }
`;
exports[`SFC compile <script setup> > imports > import dedupe between <script> and <script setup> 1`] = `
"import { x } from './x'
"
import { x } from './x'
export default {
setup(__props, { expose: __expose }) {
Expand Down Expand Up @@ -902,7 +913,9 @@ return { ref }
`;
exports[`SFC compile <script setup> > imports > should support module string names syntax 1`] = `
"import { \\"😏\\" as foo } from './foo'
"
import { \\"😏\\" as foo } from './foo'
export default {
setup(__props, { expose: __expose }) {
Expand Down Expand Up @@ -1189,7 +1202,8 @@ return (_ctx, _cache) => {
`;
exports[`SFC compile <script setup> > inlineTemplate mode > with defineExpose() 1`] = `
"export default {
"
export default {
setup(__props, { expose: __expose }) {
const count = ref(0)
Expand Down Expand Up @@ -1352,7 +1366,8 @@ return { a }
`;
exports[`SFC genDefaultAs > <script> + <script setup> 1`] = `
"const __default__ = {}
"
const __default__ = {}
const _sfc_ = /*#__PURE__*/Object.assign(__default__, {
setup(__props, { expose: __expose }) {
Expand All @@ -1367,7 +1382,8 @@ return { a }
`;
exports[`SFC genDefaultAs > <script> + <script setup> 2`] = `
"const __default__ = {}
"
const __default__ = {}
const _sfc_ = /*#__PURE__*/Object.assign(__default__, {
setup(__props, { expose: __expose }) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`defineEmits > basic usage 1`] = `
"export default {
"
export default {
emits: ['foo', 'bar'],
setup(__props, { expose: __expose, emit: __emit }) {
__expose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ return { n, get x() { return x } }
`;
exports[`defineExpose() 1`] = `
"export default {
"
export default {
setup(__props, { expose: __expose }) {
__expose({ foo: 123 })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`defineOptions() > basic usage 1`] = `
"export default /*#__PURE__*/Object.assign({ name: 'FooApp' }, {
"
export default /*#__PURE__*/Object.assign({ name: 'FooApp' }, {
setup(__props, { expose: __expose }) {
__expose();
Expand All @@ -14,7 +15,8 @@ return { }
`;

exports[`defineOptions() > empty argument 1`] = `
"export default {
"
export default {
setup(__props, { expose: __expose }) {
__expose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ return () => {}
`;

exports[`sfc reactive props destructure > defineProps/defineEmits in multi-variable declaration (full removal) 1`] = `
"export default {
"
export default {
props: ['item'],
emits: ['a'],
setup(__props, { emit: __emit }) {
Expand All @@ -192,7 +193,8 @@ return () => {}
`;

exports[`sfc reactive props destructure > multi-variable declaration 1`] = `
"export default {
"
export default {
props: ['item'],
setup(__props) {
Expand All @@ -205,7 +207,8 @@ return () => {}
`;

exports[`sfc reactive props destructure > multi-variable declaration fix #6757 1`] = `
"export default {
"
export default {
props: ['item'],
setup(__props) {
Expand All @@ -218,7 +221,8 @@ return () => {}
`;

exports[`sfc reactive props destructure > multi-variable declaration fix #7422 1`] = `
"export default {
"
export default {
props: ['item'],
setup(__props) {
Expand Down Expand Up @@ -250,7 +254,8 @@ return (_ctx, _cache) => {
`;

exports[`sfc reactive props destructure > nested scope 1`] = `
"export default {
"
export default {
props: ['foo', 'bar'],
setup(__props) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`sfc hoist static > should enable when only script setup 1`] = `
"const foo = 'bar'
"
const foo = 'bar'
export default {
setup(__props) {
Expand Down Expand Up @@ -91,7 +92,8 @@ return () => {}
`;
exports[`sfc hoist static > should not hoist a function or class 1`] = `
"export default {
"
export default {
setup(__props) {
const fn = () => {}
Expand All @@ -105,7 +107,8 @@ return () => {}
`;
exports[`sfc hoist static > should not hoist a object or array 1`] = `
"export default {
"
export default {
setup(__props) {
const obj = { foo: 'bar' }
Expand All @@ -118,7 +121,8 @@ return () => {}
`;
exports[`sfc hoist static > should not hoist a variable 1`] = `
"export default {
"
export default {
setup(__props) {
let KEY1 = 'default value'
Expand All @@ -133,7 +137,8 @@ return () => {}
`;
exports[`sfc hoist static > should not hoist when disabled 1`] = `
"export default {
"
export default {
setup(__props) {
const foo = 'bar'
Expand Down
2 changes: 0 additions & 2 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -986,8 +986,6 @@ export function compileScript(
)
}

ctx.s.trim()

return {
...scriptSetup,
bindings: ctx.bindingMetadata,
Expand Down

0 comments on commit e8e3ec6

Please sign in to comment.