typia icon indicating copy to clipboard operation
typia copied to clipboard

$tail is bad performance.

Open wvq opened this issue 1 year ago • 1 comments

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.

wvq avatar Oct 20 '24 07:10 wvq

How about challenging PR from below branch?

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

samchon avatar Oct 21 '24 06:10 samchon