npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ts-why-slow

v0.1.3

Published

Find the slowest TypeScript types from tsc --generateTrace output

Downloads

31

Readme

ts-why-slow

从 tsc --generateTrace 输出中定位最慢的 TypeScript 类型。

English docs: README.md

为什么需要它

TypeScript 大项目性能瓶颈常常来自复杂类型系统,但原始 trace 不易读。 ts-why-slow 会把原始 JSON 转成可执行结论:

  • 最慢类型 Top 列表
  • 对应位置线索(文件/行号,若可解析)
  • 可直接落地的优化建议
  • 通过 types.json 做 type id 到可读类型名映射
  • 当 trace 事件缺少位置时,回退到 types.json.firstDeclaration

安装

npm i -D ts-why-slow

用法

# 1) 先生成 trace
tsc --generateTrace ./trace-output

# 2) 再分析
npx ts-why-slow ./trace-output

# 3) 常用参数
npx ts-why-slow ./trace-output --top 20 --min-ms 5
npx ts-why-slow ./trace-output --format json > slow-types.json
npx ts-why-slow ./trace-output --format md --output ./slow-types-report.md

如果 TypeScript 6 因为弃用编译选项而阻止生成 trace,可以这样执行:

tsc --generateTrace ./trace-output --ignoreDeprecations 6.0

输出说明

默认表格会展示:

  • Duration: 该类型累计耗时
  • Share: 在总耗时中的占比
  • Hits: 出现次数(用于判断是否是高频慢点)
  • Heat: 直观热度条

CLI 参数

  • --top : 展示前 n 项(默认 10)
  • --min-ms : 过滤低于阈值的项(默认 1)
  • --format <table|json|md>: 输出模式(默认 table)
  • --output : 将报告写入文件
  • --no-color: 关闭彩色输出

推荐排查流程

  1. 先看 Share 高且 Hits 高的类型。
  2. 优先处理 DeepPartial、递归泛型、超大 union/intersection。
  3. 如果热点落在 node_modules/*.d.ts,优先检查第三方库类型是否过深或过宽。
  4. <anonymous type>--format json 查看 rawKinds,定位具体表达式热点。
  5. 重构后重新生成 trace,对比 json 输出变化。

开发与发布

npm run lint
npm test
npm run build
npm run prepublishOnly

限制

  • 不同 TypeScript 版本的 trace 字段可能有差异。
  • 即使结合 types.json,仍有部分事件无法给出直接类型名。
  • 部分事件没有精确源码位置;工具会在可能时回退到 types.json.firstDeclaration
  • 如果真正的瓶颈来自依赖库类型,位置可能落在第三方 .d.ts 文件。
  • 建议为启发式规则,应结合真实回归数据验证。