Is there any option to compile and run a ts code faster using tsc or ts-node or anything else?

27.3k Views Asked by At

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.

7

There are 7 best solutions below

1
On

Option A

  1. 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
  2. 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

0
On

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
0
On

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

5
On

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 swc integration. 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.

1
On

You could try the following, perhaps it could give you an additional boost. It uses speedy web compiler

Install these dependancies:

ts-node @swc/cli @swc/helpers @swc/core swc-loader @swc/jest

Then update your tsconfig to the following:

{
  "extends": "", // your extended configuration options here
  "ts-node": {
    "swc": true, // add speedy web compiler (typescript compiler written in rust)
    "transpileOnly": true // set to transpileOnly
  },
  "compilerOptions": {
    // your compiler options here ...
  },
  "exclude": [
    // your exclude options here ...
  ],
  "include": [
    // your include options here ...
  ],
  "references": [{
    // your reference options here ...
  }],
}

Happy coding =)

5
On

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.

Edit: I have since switched to Sucrase via [sudo] npm install -g sucrase. I just replace any ts-node occurrence with sucrase-node to get roughly a 20x speed boost in my testing. It does not need environment variables. It doesn't do any typechecking, it just makes sure you have fast dev iterations. Then you can run full tsc builds with typechecking in your editor and in CI. For my use case that is perfect, but your situation may be different. I was also able to speed up my Jest tests with @sucrase/jest-plugin.

sucrase-node ./script.ts
0
On

one way to compile fast using nodemon

  1. Install Nodemon : npm i -g nodemon

  2. Create file nodemon.json

    {
     "watch": ["src"],
     "ext": ".ts,.js",
     "ignore": [],
     "exec": "ts-node ./src/server.ts"
    }
    
  3. add command In package.json

    "start:dev": "nodemon",