[英]Does dart2js optimize const objects better?
使用dart2js编译为JavaScript时,使用const
构造函数创建的类实例是否比使用普通实例(使用new
构造函数创建)更优化?
Here is 2 tuple implementations: 这是2个元组的实现:
With a constant constructor: 使用常量构造函数:
class Tuple{
final _1, _2;
foo()=> _1 + _2;
const Tuple(this._1,this._2);
}
void main() {
var a = const Tuple(10,20);
var b = const Tuple(10,20);
print(a);
print(b);
print(a.foo());
}
With a new constructor: 使用新的构造函数:
class Tuple{
final _1, _2;
foo()=> _1 + _2;
Tuple(this._1,this._2);
}
void main() {
var a = new Tuple(10,20);
var b = new Tuple(10,20);
print(a);
print(b);
print(a.foo());
}
dart2js outputs comparison dart2js输出比较
That is how new Tuple
looks in the output: 这就是new Tuple
在输出中的外观:
main: function() {
P.print(new S.Tuple(10, 20));
P.print(new S.Tuple(10, 20));
P.print(30);
}
const Tuple
is created only once C.Tuple_10_20 = new S.Tuple(10, 20);
仅在C.Tuple_10_20 = new S.Tuple(10, 20);
才创建const Tuple
C.Tuple_10_20 = new S.Tuple(10, 20);
and used like this: 并像这样使用:
main: function() {
P.print(C.Tuple_10_20);
P.print(C.Tuple_10_20);
P.print(C.Tuple_10_20._1 + C.Tuple_10_20._2);
}
Note that in the case of new Tuple
the function call has been replaced with its return value literal but it didn't happen for the const Tuple
output. 请注意,在使用new Tuple
的情况下,函数调用已替换为其返回值文字,但对于const Tuple
输出却没有发生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.