Skip to content

Commit

Permalink
perf: replace object literals with json when generating client codes
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Jun 11, 2022
1 parent de6b43d commit 6db42f0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/@vuepress/core/src/app/prepare/preparePageData.ts
Expand Up @@ -20,7 +20,10 @@ if (import.meta.hot) {
*/
export const preparePageData = async (app: App, page: Page): Promise<void> => {
// page data file content
let content = `export const data = ${JSON.stringify(page.data, null, 2)}\n`
let content = `export const data = JSON.parse(${JSON.stringify(
JSON.stringify(page.data)
)})
`

// inject HMR code
if (app.env.isDev) {
Expand Down
4 changes: 3 additions & 1 deletion packages/@vuepress/core/src/app/prepare/prepareSiteData.ts
Expand Up @@ -20,7 +20,9 @@ if (import.meta.hot) {
*/
export const prepareSiteData = async (app: App): Promise<void> => {
let content = `\
export const siteData = ${JSON.stringify(app.siteData, null, 2)}
export const siteData = JSON.parse(${JSON.stringify(
JSON.stringify(app.siteData)
)})
`

// inject HMR code
Expand Down
Expand Up @@ -22,7 +22,9 @@ export const prepareThemeData = async (
): Promise<void> => {
// theme data file content
let content = `\
export const themeData = ${JSON.stringify(themeData, null, 2)}
export const themeData = JSON.parse(${JSON.stringify(
JSON.stringify(themeData)
)})
`

// inject HMR code
Expand Down

1 comment on commit 6db42f0

@Mister-Hope
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little explain on why it will increase performance? JSON.parse will be exceuted anyway IMO when loading the file, so I am not seeing performance improvments like reducing AST.

Please sign in to comment.