13

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
Manoj Singh
  • 131
  • 1
  • 1
  • 4
  • 1
    What version of `ts-node` are you using? The version 8.x has some known compilation performance problems. They've been discussed here: https://github.com/TypeStrong/ts-node/issues/754 – viniciusjssouza Oct 24 '19 at 15:49
  • I am using 8.4.1 version of ts-node but I think problem is due to npx it always install ts-node package. – Manoj Singh Oct 25 '19 at 09:24
  • 1
    If you cache the installed npm packages, try to run `npx` with `--no-install` flag, This will avoid installing it repeatedly and may help you catch the problem – viniciusjssouza Oct 25 '19 at 12:21

7 Answers7

33

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
kvz
  • 5,517
  • 1
  • 42
  • 33
  • I switched from 5 seconds to 2 seconds with your suggestions, thanks a lot ! – Ssh-uunen Jun 23 '21 at 14:29
  • This should be the accepted answer. Super simple and worked as a charm! You can also save it as an npm script inside `package.json`. Very useful for running small tools inside the project. – Hugo Aboud Dec 20 '21 at 00:33
  • If you use TS_NODE_TRANSPILE_ONLY, how/when do you do the type checking? – jplindstrom Dec 21 '21 at 16:29
  • I have a `start` run-script that adds the environment variables for quick iteration times. I have VSCode for giving me type checking in realtime as I write. I have a `build` run-script that not add these env vars, and before pushing I run this for good measure. It is also ran in CI of course. – kvz Dec 22 '21 at 12:46
  • Please name my daughter, you are a GOD! – user2081518 May 13 '22 at 18:01
28

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.

Steve DeWald
  • 381
  • 4
  • 4
  • 4
    This was the only one of the options that made any significant difference to run times. Thanks! – Dave Meehan Mar 29 '22 at 18:26
  • Ts-node docs on npm outlines what needs to be done: https://www.npmjs.com/package/ts-node#swc. – KJ Ang May 17 '22 at 03:50
  • But this ignores types completely. If you are using swc, you are simply bypassing the type system provided by TS. This is not what you want when you start using TS – enanone Jul 12 '23 at 12:06
6

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

Fazal Rasel
  • 4,446
  • 2
  • 20
  • 31
1

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 =)

0xe1λ7r
  • 1,957
  • 22
  • 31
0

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

Eugenio
  • 563
  • 5
  • 8
0

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",
    
Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
0

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
Akhil Mordia
  • 132
  • 1
  • 7