Skip to content

Instantly share code, notes, and snippets.

@kamauwashington
Last active September 4, 2022 23:31
Show Gist options
  • Save kamauwashington/bf84d52db863b9b3892db404b5eaf2e4 to your computer and use it in GitHub Desktop.
Save kamauwashington/bf84d52db863b9b3892db404b5eaf2e4 to your computer and use it in GitHub Desktop.

TypeScript Bits

Avoid using ts-node when performance testing

TypeScript Bits are small, easily digestible pieces of information to encourage TypeScript/JavaScript best practices. From beginner, to intermediate, to ninja, these bits are for you 😄


Estimated reading time : 2 minutes

Often times when performance testing, or comparing resource utilization (ex NodeJS vs Java), I have seen developers load testing (even one off testing) with ts-node. ts-node is an awesome piece of kit, but it is for rapid prototyping, watching, and development only, it is not a production runtime.

When performance testing, even one offs, use tsc my-nodejs-app.ts && node my-node-app

Simple test sample

const numberArray : Array<number> = [2,6,1,9,0,5,8,7,3,4];
numberArray.sort();
const heapUsed = Math.round((process.memoryUsage().heapUsed / (1024*1024)));
const cpuTime = Math.round((process.cpuUsage().user / 1000));
console.log(`Approximately ${heapUsed}MB and ${cpuTime}ms of CPU time was used by this process.`);

Using ts-node

Approximately 61MB and 1466ms of CPU time was used by this process.

Using tsc index.ts && node index

Approximately 2MB and 25ms of CPU time was used by this process.

Notes

  • heapUsed is the actual memory used during the execution of a Node process
  • memoryUsage provides rss, heapTotal, heapUsed, external, arrayBuffers metrics as well
  • the command NODE_ENV=production tsc index.ts && node index will ensure that your one off test mimics your production environment build and runtime
  • this is an illustration, not an accurate representation of resource usage
    • an APM + a performance test with sustained load over time will provide the most accurate information

A great article that discusses NodeJS profiling here..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment