简体   繁体   English

为什么dart生成的javascript文件如此庞大?

[英]Why is javascript files generated by dart is so huge?

I have the following dart code: 我有以下飞镖码:

$ cat helloworld.dart
main() => print('Hello world!');
$ 

The javascript code generated by the dart compiler for the above code is as follows: dart编译器为上面代码生成的javascript代码如下:

$ cat helloworld.dart.app.js 
function native_ListFactory__new(typeToken, length) {
  return RTT.setTypeInfo(
      new Array(length),
      Array.$lookupRTT(RTT.getTypeInfo(typeToken).typeArgs));
}
function native_ListImplementation__indexOperator(index) {
  return this[index];
}
function native_ListImplementation__indexAssignOperator(index, value) {
  this[index] = value;
}
function native_ListImplementation_get$length() {
  return this.length;
}
function native_ListImplementation__setLength(length) {
  this.length = length;
}
function native_ListImplementation__add(element) {
  this.push(element);
}
function native_BoolImplementation_EQ(other) {
  return typeof other == 'boolean' && this == other;
}
function native_BoolImplementation_toString() {
  return this.toString();
}

<snapped>

var static$uninitialized = {};
var static$initializing = {};
function $inherits(child, parent) {
  if (child.prototype.__proto__) {
    child.prototype.__proto__ = parent.prototype;
  } else {
    function tmp() {};
    tmp.prototype = parent.prototype;
    child.prototype = new tmp();
    child.prototype.constructor = child;
  }
}
isolate$inits.push(function(){
  isolate$current.Duration$DartMILLISECONDS_PER_MINUTE$field = static$uninitialized;
  isolate$current.Duration$DartMILLISECONDS_PER_HOUR$field = static$uninitialized;
  isolate$current.Duration$DartMILLISECONDS_PER_DAY$field = static$uninitialized;
  isolate$current.Duration$DartSECONDS_PER_HOUR$field = static$uninitialized;
  isolate$current.Duration$DartSECONDS_PER_DAY$field = static$uninitialized;
  isolate$current.Duration$DartMINUTES_PER_DAY$field = static$uninitialized;
}
);
RunEntry(unnamedd9297f$main$member, this.arguments ? (this.arguments.slice ? [].concat(this.arguments.slice()) : this.arguments) : []);
$

And the size of helloworld.dart.app.js is 102k! helloworld.dart.app.js的大小是102k!

When ran in optimize mode, it generated the following javascript - helloworld.dart.js which is of size 20k 在优化模式下运行时,它生成了以下javascript - helloworld.dart.js ,大小为20k

$ cat helloworld.dart.js 
var e;function f(a,b){if(b>=0&&b<a.length)return b;h(i(b))};var j={},k={};function aa(a,b,c){if(b)a.g=function(){return b.call(c)}}function ba(a,b,c,d){function g(b,g,t,m){return a.call(c,d,b,g,t,m)}aa(g,b,c);return g}function l(a,b){if(a.prototype.__proto__)a.prototype.__proto__=b.prototype;else{var c=function(){};c.prototype=b.prototype;a.prototype=new c;a.prototype.constructor=a}}function ca(a,b){return typeof a=="number"&&typeof b=="number"?a+b:a.na(b)}function da(a){a/=4;return a<0?Math.ceil(a):Math.floor(a)}
function o(a,b){if(a===void 0)return b===void 0;else if(typeof a==typeof b&&typeof a!="object")return a===b;return a.G(b)}function h(a){a&&typeof a=="object"&&Error.captureStackTrace&&Error.captureStackTrace(a);throw a;}function p(){var a=new q;a.f=s("ya",ea);a.va="";a.qa="";a.N=[];h(a)}var u={d:0};

<snapped>

y.push(function(){x.fb=j;x.eb=j;x.gb=j});y.push(function(){x.Ta=j;x.Sa=j;x.Ra=j;x.Wa=j;x.Va=j;x.Ua=j});(function(a,b){if(!A){var c=new ya;oa=c;sa(c,function(){a(b)});Ea();x=c}})(function(){return qb()(1,u,"Hello world!")},this.arguments?this.arguments.slice?[].concat(this.arguments.slice()):this.arguments:[]);
$

Why is the javascript code which was generated by a dart compiler so huge? 为什么由dart编译器生成的javascript代码如此庞大?

What problem are they trying to solve by generating such huge javascript files? 他们试图通过生成如此巨大的JavaScript文件来解决什么问题?

Side Note: The javascript files were so huge, SO threw the following error: 附注:javascript文件太大了,SO引发了以下错误:

Oops! 哎呀! Your question couldn't be submitted because: body is limited to 30000 characters; 您的问题无法提交,因为:正文限制为30000个字符; you entered 140984 你输入了140984

What problem are they trying to solve by generating such huge javascript files? 他们试图通过生成如此巨大的JavaScript文件来解决什么问题?

The problem of balancing Dart to be optimal in the majority of cases, rather than just this one personal, specific, contrived, useless example program that nobody would ever seriously try to use in a production project. 在大多数情况下,平衡Dart是最佳的问题,而不仅仅是这个个人的,具体的,人为的,无用的示例程序,没有人会认真地尝试在生产项目中使用。

If you would like to get even a better JavaScript, try the Frog compiler instead of DartC. 如果您想获得更好的JavaScript,请尝试使用Frog编译器而不是DartC。 Frog ist written in Dart itself. 青蛙是用Dart本身写的。

http://turbomanage.wordpress.com/2011/12/09/dart-dev-mode-cometh/ http://turbomanage.wordpress.com/2011/12/09/dart-dev-mode-cometh/

Note, this blogpost above is a bit outdated. 注意,上面这篇博文有点过时了。 You can meanwhile use the Dart SDK for Frog: http://gsdview.appspot.com/dart-editor-archive-continuous/3058/ 您可以同时使用Dart SDK for Frog: http//gsdview.appspot.com/dart-editor-archive-continuous/3058/

This post might also be of interest for you, Seth shows how the Frog generated JS looks like: http://blog.sethladd.com/2011/12/10-lessons-from-porting-javascript-to.html 这篇文章也可能对你有用,Seth展示了Frog生成JS的样子: http//blog.sethladd.com/2011/12/10-lessons-from-porting-javascript-to.html

Here is how you can enable Frog in current Editor: https://groups.google.com/a/dartlang.org/group/misc/msg/5dfe04c69ed0fed3 以下是在当前编辑器中启用Frog的方法: https//groups.google.com/a/dartlang.org/group/misc/msg/5dfe04c69ed0fed3

Why is the javascript code which was generated from a dart code by a dart compiler is so huge? 为什么dart编译器从dart代码生成的javascript代码如此庞大?

Because it includes a Dart runtime. 因为它包含Dart运行时。

What problem are they trying to solve by generating such huge javascript files? 他们试图通过生成如此巨大的JavaScript文件来解决什么问题?

The problem of running code that is not JavaScript in a browser. 在浏览器中运行非JavaScript的代码的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM