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
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.`);
Approximately 61MB and 1466ms of CPU time was used by this process.
Approximately 2MB and 25ms of CPU time was used by this process.
- 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..