Add experimental option to cache the .length access in downlevel for-of emit.

This commit is contained in:
Cyrus Najmabadi
2015-03-13 16:15:25 -07:00
parent 4a9187172e
commit d8d4719765
18 changed files with 163 additions and 42 deletions

View File

@@ -146,6 +146,12 @@ module ts {
description: Diagnostics.Preserve_new_lines_when_emitting_code,
experimental: true
},
{
name: "cacheDownlevelForOfLength",
type: "boolean",
description: "Cache length access when downlevel emitting for-of statements",
experimental: true,
},
{
name: "target",
shortName: "t",

View File

@@ -3583,6 +3583,8 @@ module ts {
let counter = createTempVariable(node, /*forLoopVariable*/ true);
let rhsReference = rhsIsIdentifier ? <Identifier>node.expression : createTempVariable(node, /*forLoopVariable*/ false);
var cachedLength = compilerOptions.cacheDownlevelForOfLength ? createTempVariable(node, /*forLoopVariable:*/ false) : undefined;
// This is the let keyword for the counter and rhsReference. The let keyword for
// the LHS will be emitted inside the body.
emitStart(node.expression);
@@ -3602,14 +3604,30 @@ module ts {
emitNodeWithoutSourceMap(node.expression);
emitEnd(node.expression);
}
if (cachedLength) {
write(", ");
emitNodeWithoutSourceMap(cachedLength);
write(" = ");
emitNodeWithoutSourceMap(rhsReference);
write(".length");
}
write("; ");
// _i < _a.length;
emitStart(node.initializer);
emitNodeWithoutSourceMap(counter);
write(" < ");
emitNodeWithoutSourceMap(rhsReference);
write(".length");
if (cachedLength) {
emitNodeWithoutSourceMap(cachedLength);
}
else {
emitNodeWithoutSourceMap(rhsReference);
write(".length");
}
emitEnd(node.initializer);
write("; ");

View File

@@ -1560,6 +1560,7 @@ module ts {
watch?: boolean;
stripInternal?: boolean;
preserveNewLines?: boolean;
cacheDownlevelForOfLength?: boolean;
[option: string]: string | number | boolean;
}