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

$tail is bad performance. #1335

Open
wvq opened this issue Oct 20, 2024 · 1 comment
Open

$tail is bad performance. #1335

wvq opened this issue Oct 20, 2024 · 1 comment
Assignees
Labels
good first issue Good for newcomers

Comments

@wvq
Copy link

wvq commented Oct 20, 2024

import typia from 'typia'

interface User {
  id?: number
  name?: string
  avatar?: string
  mobile?: string
}

const user: User = {
  id: 1,
  name: 'Bob',
  avatar: 'https://avatar.com/bob',
  mobile: '12345678901'
}

let stringify = typia.json.createStringify<User>()
performance.mark('test')

for (let i = 0; i < 1e7; i++) {
  stringify(user)
}

let result = performance.measure('test')

console.log(result.duration) // = 3066.775059

The $tail function execution time is positively correlated with length of string because of str[str.length -1].
The longer of the string, the longer time it takes.

export const $tail = (str: string): string =>
  str[str.length - 1] === "," ? str.substring(0, str.length - 1) : str;

// I think it should be 
export const $tail = (str: string, hasLastExpresstion: boolean) => 
  hasLastExpresstion ? str : str.substring(0, -1)

As the generator knows the last prop expression is undefined === input.mobile, just pass second argument undefined !== input.mobile to $tail.

After do this, result.duration reduce to 1733.192805 on my machine.

@samchon
Copy link
Owner

samchon commented Oct 21, 2024

How about challenging PR from below branch?

https://github.com/samchon/typia/tree/v7.0

@samchon samchon added the good first issue Good for newcomers label Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants