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 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-check-perf

v0.0.0

Published

Measuring and comparing type-checking speed of TS samples

Downloads

2

Readme

ts-check-perf

A tool for measuring and comparing type-checking speed of TS samples.

npm i -D ts-check-perf

It runs TS type-checked many times over the same samples to measure type-checking performance.

Optionally, define a preparation code that is loaded by TS only once.

Samples can import files of your project, project files are also loaded by TS only once, so only the performance of samples is measured.

Defining samples

For example, we want to know which is faster: extending type with new properties, or interface.

Define a common code that is not wanted to be measured as a preparation:

const preparation = `
  type Type = {
    one: 1
    two: 2
    three: 3
  }
  
  interface Interface = {
    one: 1
    two: 2
    three: 3
  }
`;

Next, we can define our samples, one will extend the type, and the second will extend the interface.

Types in samples must be exported, otherwise TS compiler will throw an error about name collision.

Sample code can be defined as object where keys are sample names and values are TS code:

const samples = {
  type: `
    export type Obj = {
      one: 1
      two: 2
      three: 3
    }
  `,
  interface: `
    export interface Obj {
      one: 1
      two: 2
      three: 3
    }
  `,
};

Or, samples can be defined as array, samples names will be 1st, 2nd, 3rd:

const samples = [
  `
    export type Extended = Type & {
      four: 4
      five: 5
      six: 6
    }
  `,
  `
    export interface Extended extends Interface {
      four: 4
      five: 5
      six: 6
    }
  `,
];

Measure time

measureTime type checks first sample, second, first, second, and does so 1000 times for each sample.

import { measureTime } from "ts-check-perf";

measureTime({
  preparation,
  samples,
})

Outputs:

1st.ts (slowest): 35ms
2nd.ts +1.06x (fastest): 33ms

Measurement numbers below 1 second should not be trusted, such results will be different on every benchmark run. Changing it to type-check million times:

measureTime({
  runTimes: 1_000_000,
  preparation,
  samples,
})
1st.ts (slowest): 2899ms
2nd.ts +1.22x (fastest): 2385ms

Measure speed

measureTime type checks first sample, second, first, second, and continues for 100ms for each sample, measuring how many operations per second each sample gives.

import { measureSpeed } from "ts-check-perf";

measureSpeed({
  preparation,
  samples,
})
1st.ts (slowest): 289k ops/s
2nd.ts +1.4x (fastest): 404.1k ops/s

Change the duration by setting durationMs:

measureSpeed({
  durationMs: 1000,
  preparation,
  samples,
})
1st.ts (slowest): 332.4k ops/s
2nd.ts +1.37x (fastest): 454.9k ops/s

benchmark.js

You can use ts-check-perf in together with other benchmarking tools which provides more metrics.

import { setupTsBenchmark } from "ts-check-perf";
import * as Benchmark from 'benchmark'

const { typeChecker, sourceFiles } = setupTsBenchmark({
  preparation,
  samples,
})

const suite = new Benchmark.Suite()

suite
  .add('type', () => {
    typeChecker.checkFileForBenchmark(sourceFiles[0]);
  })
  .add('interface', () => {
    typeChecker.checkFileForBenchmark(sourceFiles[1]);
  })
  .on('cycle', (e) => {
    console.log(String(e.target));
  })
  .on('complete', () => {
    console.log('Fastest is ' + suite.filter('fastest').map('name').join(', '));
  })
  .run()
type x 349,530 ops/sec ±6.94% (74 runs sampled)
interface x 286,340 ops/sec ±17.00% (63 runs sampled)
Fastest is type

Common parameters

Set log: false to measureTime and measureSpeed to disable logging, they return a result Record<'sample name', number> you can use programmatically.

By default, this tool is looking for tsconfig.json for configuring TS, you can change this file name by setting tsconfigName parameter to options of measureTime, measureSpeed, setupTsBenchmark.

ESM

This tools works by patching TS source code on-the-fly: it reads TypeScript's compiler code from node_modules, injects own function into it, and saves the result to require.cache.

Such trick only works with CommonJS module system, there is no official way to do this for ESM, so this library doesn't support ESM and have no plans for it.

Bun

Interesting observation, running the example from above with measureSpeed:

$ node -v
v20.8.1
$ node bench.js
1st.ts (slowest): 323.5k ops/s
2nd.ts +1.27x (fastest): 412.1k ops/s

Running the same file with Bun:

$ bun -v
1.0.15
$ bun bench.js
1st.ts (slowest): 562.0k ops/s
2nd.ts +1.12x (fastest): 631.9k ops/s