TypeScript 性能(asm.js,闭包编译器)和开销

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20820596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:52:45  来源:igfitidea点击:

TypeScript performance (asm.js, closure compiler) and overhead

javascriptperformancetypescriptgoogle-closure-compilerasm.js

提问by m_vdbeek

I'm looking into using TypeScriptcombined with a client-side MVC (most likely Backbone.js+ Marionette.jsor Ember.js) for future projects and therefor have a few questions related to performance and optimisation :

我正在考虑在未来的项目中TypeScript结合使用客户端 MVC(最有可能是Backbone.js+Marionette.jsEmber.js),因此有一些与性能和优化相关的问题:

  • How does TypeScriptoutput compare to native JavaScript performance wise ?

  • Since asm.jsis a subset of JavaScript, would it be possible to convert TypeScriptcode into asm.jscode ? If yes, is it already possible ?

  • Is it possible and still useful to create builds of AMD projects that use TypeScriptwith Google Closure compiler?

  • How much overhead does TypeScriptadd on average, file-size wise ?

  • When using lightweight libraries like Backbone.jsin a small project for example. Does it make sense to use TypeScriptin regards to file size ?

  • TypeScript输出与原生 JavaScript 性能相比如何?

  • 由于asm.js是 JavaScript 的一个子集,是否可以将TypeScript代码转换为asm.js代码?如果是,是否已经有可能?

  • 创建TypeScript与 一起使用的 AMD 项目构建是否可能并且仍然有用Google Closure compiler

  • TypeScript平均而言,文件大小会增加多少开销?

  • 例如Backbone.js,在小型项目中使用轻量级库时。TypeScript在文件大小方面使用是否有意义?

I like the added benefits of TypeScriptbut I don't want to sacrifice performance for coding style and typing.

我喜欢 的附加好处,TypeScript但我不想牺牲编码风格和打字的性能。

Any articles / books about using TypeScriptin big projects, especially related to performance, optimisations and builds are very welcome !

任何关于TypeScript在大型项目中使用的文章/书籍,特别是与性能、优化和构建相关的文章/书籍都非常受欢迎!

Thank you in advance !

先感谢您 !

回答by jJ'

We evaluated and tested TypeScript thoroughly in our team and other teams use it already, so here is my experience:

我们在我们的团队中彻底评估和测试了 TypeScript,其他团队已经在使用它,所以这是我的经验:

  • TypeScript is a superset of JavaScript and it mostly translates 1:1 to JavaScript without any significant performance compromises, so if you know how to write efficient JavaScript, you know how to write efficient TypeScript. One of the not so efficient features is inheritance, which is "emulated" using JavaScript prototypes and produces more code than one would write normally in JavaScript. So use inheritance with caution. You can look at generated JavaScript to see whether your constructs are compiled efficiently enough for your case.
  • Compiling typescript to asm.js is the same problem as compiling JavaScript to asm.js - you would need to emulate the features asm.js lacks compared to full javascript... If you need some parts in asm.js you would probably need to write them yourself or compile from some more appropriate (less dynamic) language by emscripten and such.
  • TypeScript has some AMD support, but I cannot speak about google closure support... Since it tries to accomplish similar thing very differently (types and metainfo in comments instead in a new syntax), I do not think that they are very compatible to use maximum of both...
  • File size is not really an issue, it is very similar to file size of a readable JavaScript, unless you use inheritance a lot
  • Using TypeScript with backbone and other libraries has one benefit compared to JavaScript; most popular libraries already have type definition files for TypeScript, so you get autocompletion and type checking almost for free. File size difference is not really an issue compared to well written JavaScript.
  • TypeScript 是 JavaScript 的超集,它主要将 1:1 转换为 JavaScript,而没有任何显着的性能妥协,所以如果你知道如何编写高效的 JavaScript,你就知道如何编写高效的 TypeScript。一个不太有效的特性是继承,它使用 JavaScript 原型“模拟”并产生比正常用 JavaScript 编写的代码更多的代码。所以要谨慎使用继承。您可以查看生成的 JavaScript,看看您的构造是否针对您的情况进行了足够有效的编译。
  • 将 typescript 编译为 asm.js 与将 JavaScript 编译为 asm.js 的问题相同 - 与完整的 javascript 相比,您需要模拟 asm.js 缺少的功能......如果您需要 asm.js 中的某些部分,您可能需要自己编写它们或通过 emscripten 等从一些更合适(不太动态)的语言编译。
  • TypeScript 有一些 AMD 支持,但我不能谈论 google 闭包支持...因为它试图以非常不同的方式完成类似的事情(注释中的类型和元信息而不是新语法),我认为它们使用起来不太兼容两者中的最大值...
  • 文件大小不是真正的问题,它与可读 JavaScript 的文件大小非常相似,除非您经常使用继承
  • 与 JavaScript 相比,将 TypeScript 与主干和其他库一起使用有一个好处;大多数流行的库已经有 TypeScript 的类型定义文件,所以你几乎可以免费获得自动完成和类型检查。与编写良好的 JavaScript 相比,文件大小差异并不是真正的问题。

TypeScript is still quite young and many things we wanted to have (JSLint, code coverage, TDD, BDD tools, ...) were missing at the time. In addition, there were several bugs in the compiler (that have been fixed afterwards), so we did not chose to use it, but no point from your list showed as a real turn-down for us...

TypeScript 还很年轻,我们想要的很多东西(JSLint、代码覆盖率、TDD、BDD 工具……)当时都没有。此外,编译器中有几个错误(后来已修复),因此我们没有选择使用它,但是您的列表中没有一点显示为我们真正拒绝...

Update: To see the potential of TypeScript you can look at Visual Studio Online "Monaco". What they did there is pretty impressive see Getting started with TypeScriptfor quick intro

更新:要了解 TypeScript 的潜力,您可以查看 Visual Studio Online“Monaco”。他们在那里所做的令人印象深刻,请参阅TypeScript 入门以快速介绍