'Is there any option to compile and run a ts code faster using tsc or ts-node or anything else?
Currently we have client server application (for competitive coding purposes) where client hit compile and run and sends ts code to server where ts code is stored in a file and run locally with testcases on server and output is returned to client with test case pass/fail result. But running ts file is very slow and this is taking so much time.
I am using ts-node in transpileonly mode to compile and run the file locally in server.
eg. npx ts-node -T tsFileName.ts
Our requirement is fastened to compile and run time of ts code.
Solution 1:[1]
I added these environment variables and startup time went from seconds to, milliseconds probably:
TS_NODE_FILES=true TS_NODE_TRANSPILE_ONLY=true ts-node ./script.ts
Consider installing ts-node via [sudo] npm install -g typescript ts-node, then you avoid the extra steps that npx takes to make sure ts-node is installed every time.
Solution 2:[2]
TS-Node Official Recommendations
The official TS-Node docs outline several performance recommendations, some of which others have commented on.
https://typestrong.org/ts-node/docs/performance/
However, I'm surprised no one has mentioned the SWC integration! From the docs:
Use our
swcintegration. This is by far the fastest option
Speedy Web Compiler (SWC)
SWC, or Speedy Web Compiler, is a transpiler for JavaScript/TypeScript written completely in Rust. As such, it's much faster than anything you're going to get out of alternatives like tsc or babel.
According to the SWC website (https://swc.rs/):
SWC is 20x faster than Babel on a single thread and 70x faster on four cores.
Set up with TS-Node
Add the SWC core library to your project:
npm i -D @swc/core
And add the following to your tsconfig.json:
{
"ts-node": {
"swc": true
}
}
And you're good to go! Enjoy blazingly fast transpiling.
Solution 3:[3]
You can use esbuild-runner which seems much much faster.
npm install -g esbuild-runner
npm install -g esbuild
And run the code
esr src/index.ts
Solution 4:[4]
Option A
- Try to generate the TS code in different files, for example: splitting dynamically each function in a separated file, or asking the user to "upload" different ts files wich less code each one, also you could dynamically check and restrict the number of lines per function or file
- Just transpile the file which has been changed (this part could be easily done using watch compiler option)
Option B
Try playing with some compiler options
some interesting ones: incremental:true, noEmit: true, strict:false, skipLibCheck:true
Solution 5:[5]
one way to compile fast using nodemon
Install Nodemon :
npm i -g nodemonCreate file
nodemon.json{ "watch": ["src"], "ext": ".ts,.js", "ignore": [], "exec": "ts-node ./src/server.ts" }add command In
package.json"start:dev": "nodemon",
Solution 6:[6]
If anyone is still looking for better ways to do this in 2021/22, there is a much simpler way to achieve this. Below are the steps:
Add the below docker-compose file to your project:
version: '3'
services:
#this will watch typescript files and keep building them
#replace typescript version in command with the one you need
tsc:
image: 'node:14-alpine'
volumes:
- ./:/myapp
working_dir: /myapp
command: >
sh -c "npm install typescript@^3.9.3 -g && tsc --watch"
#this will watch build files and restart your app if any changes
#replace folders according to your dir structure
#replace app.js in command with appropriate filname like index.js
nodemon:
image: 'node:14-alpine'
volumes:
- ./:/myapp
- ./.env:/myapp/build/.env
working_dir: /myapp/build
network_mode: host
command: >
sh -c "npm install nodemon -g && nodemon app.js"
What this does is, it will run typescript watcher and nodemon in parallel and continuously build your code in milliseconds.
Once you add the above file to your project, just run
docker-compose up
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Steve DeWald |
| Solution 3 | Fazal Rasel |
| Solution 4 | |
| Solution 5 | Saurabh Mistry |
| Solution 6 | Akhil Mordia |
