Skip to content

Commit 8ed846c

Browse files
authored
Reuse start position in binarySearchKey (#49641)
* Reuse start position in binarySearchKey * Kick out early if end < position
1 parent 2bc91a6 commit 8ed846c

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/services/utilities.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,13 +1147,20 @@ namespace ts {
11471147
// position and whose end is greater than the position.
11481148

11491149

1150+
// There are more sophisticated end tests later, but this one is very fast
1151+
// and allows us to skip a bunch of work
1152+
const end = children[middle].getEnd();
1153+
if (end < position) {
1154+
return Comparison.LessThan;
1155+
}
1156+
11501157
const start = allowPositionInLeadingTrivia ? children[middle].getFullStart() : children[middle].getStart(sourceFile, /*includeJsDoc*/ true);
11511158
if (start > position) {
11521159
return Comparison.GreaterThan;
11531160
}
11541161

11551162
// first element whose start position is before the input and whose end position is after or equal to the input
1156-
if (nodeContainsPosition(children[middle])) {
1163+
if (nodeContainsPosition(children[middle], start, end)) {
11571164
if (children[middle - 1]) {
11581165
// we want the _first_ element that contains the position, so left-recur if the prior node also contains the position
11591166
if (nodeContainsPosition(children[middle - 1])) {
@@ -1181,13 +1188,16 @@ namespace ts {
11811188
return current;
11821189
}
11831190

1184-
function nodeContainsPosition(node: Node) {
1185-
const start = allowPositionInLeadingTrivia ? node.getFullStart() : node.getStart(sourceFile, /*includeJsDoc*/ true);
1191+
function nodeContainsPosition(node: Node, start?: number, end?: number) {
1192+
end ??= node.getEnd();
1193+
if (end < position) {
1194+
return false;
1195+
}
1196+
start ??= allowPositionInLeadingTrivia ? node.getFullStart() : node.getStart(sourceFile, /*includeJsDoc*/ true);
11861197
if (start > position) {
11871198
// If this child begins after position, then all subsequent children will as well.
11881199
return false;
11891200
}
1190-
const end = node.getEnd();
11911201
if (position < end || (position === end && (node.kind === SyntaxKind.EndOfFileToken || includeEndPosition))) {
11921202
return true;
11931203
}

0 commit comments

Comments
 (0)