deno vs ts-node:有什么区别

I'm working on a relative large typescript project, I'm using ts-node to run node testing and examples. As far as I understand, ts-node will compile ts files to js files and execute.

Recently I heard about deno, which is a typescript runtime. I tried a few examples in typescript, which works using ts-node. I ran the example with deno, there were many compile messages printed in the console, then execute the code. And later I found there's cache files in /username/.deno. I don't feel the deno execution is faster than ts-node

It seems both deno and ts-node will compile and run using cache. What's the difference between them?

评论
  • nrerum
    nrerum 回复

    TL; DR

    Deno更像Node而不是ts-node,即它是基于V8的JS运行时。与Node不同,Deno包含TypeScript编译器。 Deno不是Node / npm生态系统的一部分。

    ts-node是一个Node.js模块,它使用TypeScript编译器来转换TypeScript代码并在Node中运行它。 ts-node是Node / npm生态系统的一部分。

    迪诺很快。见下文。

    Deno和ts-node的相似之处

    • 它们都运行TypeScript代码
    • 它们都可以在Linux,Mac和Windows上运行(但ts节点也可以在SmartOS和AIX上运行)
    • 它们都使用Google V8 JavaScript引擎(ts-node通过其在后台使用的节点)

    Deno和ts-node的差异

    ts节点

    • ts-node是一个Node.js模块
    • 它是用Node.js编写的
    • 它与npm一起安装
    • 它使用TypeScript编译器作为对等依赖项
    • 它安装自己的依赖项
    • 作为运行时,它使用使用libuv用C ++编写的Node

    天野

    • deno是一个独立的可执行文件
    • 它不使用Node.js
    • 它作为一个二进制文件分发
    • 它包含TypeScript编译器作为V8快照
    • 它没有依赖性
    • 它是使用Tokio用Rust编写的运行时

    到期

    ts节点

    ts-node依赖于Node.js运行时,因此可以将其包含在此处:

    • Node.js于2009年发布,最新的LTS版本是10.15.3
    • npm于2010年发布,Node LTS中包含的版本为6.4.1
    • ts-node于2015年发布,最新版本为8.0.3

    天野

    Deno本身是一个运行时,因此它不使用其他任何东西:

    • Deno于2018年发布,最新版本是0.3.6

    人气度

    的GitHub:

    堆栈溢出:

    图书馆

    ts节点

    您可以使用npm上所有可用的Node库

    (目前,npm上有955,263个软件包,并非全部用于Node,但数量仍然很多)

    The Node libraries that are available on npm even if they were originally written in TypeScript are usually published in a form transpiled to JavaScript with additional type definitions in *.d.ts files (included in the npm package or installed separately from the @types namespace).

    天野

    There are 55 third-party modules on https://deno.land/x/ and 56 libraries and tools on https://github.com/denolib/awesome-deno#modules (I didn't check if all are the same)

    Deno库只是TypeScript文件。

    安装差异

    ts节点

    • you install Node.js
    • you install typescript and ts-node with their dependencies with npm
      • npm install typescript ts-node
      • it installs 10 npm modules and puts 44MB in 212 files into node_modules

    天野

    您的代码差异

    ts节点

    • your code works the same as if it were transpiled with tsc and run with node (because it is under the hood)
    • you can use the Node API
    • you can use all built-in Node modules
    • you can use modules from npm
    • you can import files using relative paths (usually without .ts suffix)
    • you can import the dependencies installed with npm (or yarn) in node_modules

    天野

    • your code doesn't work the same as in Node (because it isn't run with Node)
    • you use the Deno API
    • you can use the Deno built-in modules
    • you can use other Deno modules that are available
    • you can import files using relative paths (always with .ts suffix!)
    • you can import URLs directly from the Web (no need for npm install)

    例子

    这是发布并使用TypeScript编写的最小库的示例。

    使用Node和ts-node创建和使用TypeScript库

    这就是我现在在以下示例项目中正在做的事情:

    https://github.com/rsp/node-ts-hello

    创建库:

    1. find a name that is free on npm (no longer enough, see below)
    2. create repo on GitHub
    3. create package.json with npm init
    4. install TypeScript compiler with npm install typescript
    5. decide if you're keeping package-lock.json in the repo (there are pros and cons)
    6. create a src dir where you will keep TypeScript files
    7. add hello.ts to src
    8. add tsconfig.json file and make sure to:
      • add "src/**/*" to "include"
      • add dependencies and your own types to "paths"
      • add "outDir": "dist" to put the JS files in a known place
      • add the dist directory to .gitignore so that compiled files are not in git
      • add the same as in .gitignore but without dist in .npmignore
        (or otherwise you will not publish the most important files, see below)
      • add "declaration": true so you have *.d.ts files generated
    9. add "main": "dist/hello.js" in package.json (note the "js" suffix)
    10. add "types": "dist/hello.d.ts" in package.json (note the "ts" suffix)
    11. add "build": "tsc" to package.json (watch out for redundant files, see below)
    12. login with npm login (you shouldn't be logged in all the time - see: Now Pushing Malware: NPM package dev logins slurped by hacked tool popular with coders)
    13. compile the project with npm run build
    14. publish the package with npm publish
      • when you get npm ERR! publish Failed PUT 401 you need to login with npm login
      • when you get npm ERR! publish Failed PUT 403 your package may be "too similar to existing packages" - try renaming it in package.json, rename the repo and update all liks to readme, issues itp. in package.json
    15. logout from npm with npm logout
    16. see your ~/.npmrc and make sure you have nothing like this left:
      • //registry.npmjs.org/:_authToken=...

    Using the library in other project using ts-node

    1. create a new directory
    2. create a package.json file with npm init
      • (so that you can install dependencies locally for your new program)
    3. install our library with npm install node-ts-hello
    4. optionally install ts-node with npm install typescript ts-node
      • (unless it's installed globally)
    5. add hi.ts file that imports our library with:
      • import { hello } from 'node-ts-hello';
      • hello('TS');
    6. run it with npx ts-node hi.ts (if ts-node was installed locally) or ts-node hi.ts (if ts-node was installed globally)
      • if you get errors, see below

    Potential problems: I simplified the above a little bit, my actual process of creating that library is described here.

    使用Deno创建和使用TypeScript库

    这就是我现在在以下示例项目中正在做的事情:

    https://github.com/rsp/deno-hello

    创建库:

    1. create repo on GitHub
    2. put hello.ts in the repo

    使用库:

    1. Create a file hi.ts with the contents:
      • import { hello } from 'https://raw.githubusercontent.com/rsp/deno-hello/master/hello.ts';
      • hello('TS');
    2. Run your program with deno run hi.ts

    第一次运行将打印:

    $ deno run hi.ts 
    Compiling file:///Users/rsp/talks/deno/hello-deno-test/hi.ts
    Downloading https://raw.githubusercontent.com/rsp/deno-hello/master/hello.ts
    Compiling https://raw.githubusercontent.com/rsp/deno-hello/master/hello.ts
    Hello, TS!
    

    第二次运行:

    $ deno run hi.ts 
    Hello, TS!
    

    If you change hi.ts it will be recompiled but the dependencies will not get downloaded again:

    $ deno run hi.ts 
    Compiling file:///Users/rsp/talks/deno/hello-deno-test/hi.ts
    Hello, TS!
    

    (Note that touch hi.ts will not be enough, you need to make the actual changes because Deno checks the file checksum, not the timestamp.)

    速度

    ts节点

    The speed of starting the ts-node version of our hi.ts from the examples above:

    $ time npx ts-node hi.ts 
    Hello, TS!
    
    real    0m0.904s
    user    0m1.300s
    sys     0m0.083s
    

    这是在已经安装依赖项之后,以及运行几次以确保所有缓存都能正常工作之后。 差不多一秒钟。

    天野

    The speed of starting the Deno version of our hi.ts from the examples above:

    $ time deno run hi.ts 
    Hello, TS!
    
    real    0m0.028s
    user    0m0.010s
    sys     0m0.015s
    

    这也是在已经安装依赖项之后以及在运行几次以确保所有缓存都能正常工作之后。

    速度提高了32倍以上。

    摘要

    Deno should be compared more with Node than with ts-node because Deno is an entirely new runtime while ts-node is a module for Node so your program run with ts-node really use the Node runtime.

    这是一个非常年轻的项目,但是已经吸引了很多人。它没有Node那样多的文档或库,但它意味着它可能是参与其中的最佳时机,因为当它变得更流行时,我认为由于很多原因,这将超出此答案的范围。像Node一样,将需要在市场上拥有丰富经验的人。

    程序启动速度已经非常令人印象深刻,我希望在那里能有更多的改进。

    The development speed of using single files with no need for configuration like package.json or node_modules together with a possibility to import dependencies directly from URLs (like on the frontend) will make it possible to work in a different way both for the end user code and for the libraries. We'll see how it all works in practice but it already looks promising.

  • 韦小宝
    韦小宝 回复

    ts-node是基于Node的,而Deno是完全不同的服务器端新运行时,其API,模块系统,安全模型等方面的设计都有了变化(这更好地反映了ES6之后的发展)。另外,TypeScript编译器直接位于单个Deno可执行文件内部(通过V8快照),因此启动时间应更短。