From 0003216069f09e38e1893c9267c6d1c40afcbfa6 Mon Sep 17 00:00:00 2001 From: Francesco Trotta Date: Fri, 7 Apr 2023 07:01:40 +0200 Subject: [PATCH 1/3] perf: Binary search in token store `utils.search` --- lib/source-code/token-store/utils.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/source-code/token-store/utils.js b/lib/source-code/token-store/utils.js index 859831916ea..896d546d0b7 100644 --- a/lib/source-code/token-store/utils.js +++ b/lib/source-code/token-store/utils.js @@ -30,9 +30,20 @@ function getStartLocation(token) { * @returns {number} The found index or `tokens.length`. */ exports.search = function search(tokens, location) { - const index = tokens.findIndex(el => location <= getStartLocation(el)); + for (let minIndex = 0, maxIndex = tokens.length - 1; minIndex <= maxIndex;) { + const index = (minIndex + maxIndex) / 2 | 0; + const token = tokens[index]; - return index === -1 ? tokens.length : index; + if (location <= getStartLocation(token)) { + if (index === minIndex) { + return index; + } + maxIndex = index; + } else { + minIndex = index + 1; + } + } + return tokens.length; }; /** From c2d681ed6425662b67d239d31a7aa1b64a0aef89 Mon Sep 17 00:00:00 2001 From: Francesco Trotta Date: Fri, 7 Apr 2023 20:15:21 +0200 Subject: [PATCH 2/3] Use `Math.trunc` --- lib/source-code/token-store/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/source-code/token-store/utils.js b/lib/source-code/token-store/utils.js index 896d546d0b7..ae8449be713 100644 --- a/lib/source-code/token-store/utils.js +++ b/lib/source-code/token-store/utils.js @@ -31,7 +31,7 @@ function getStartLocation(token) { */ exports.search = function search(tokens, location) { for (let minIndex = 0, maxIndex = tokens.length - 1; minIndex <= maxIndex;) { - const index = (minIndex + maxIndex) / 2 | 0; + const index = Math.trunc((minIndex + maxIndex) / 2); const token = tokens[index]; if (location <= getStartLocation(token)) { From 55dbf530eab1a493d3554d9520d2861fa66be364 Mon Sep 17 00:00:00 2001 From: Francesco Trotta Date: Mon, 10 Apr 2023 13:29:35 +0200 Subject: [PATCH 3/3] Switch back to `| 0`, inline `getStartLocation` --- lib/source-code/token-store/utils.js | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/lib/source-code/token-store/utils.js b/lib/source-code/token-store/utils.js index ae8449be713..3e01470321a 100644 --- a/lib/source-code/token-store/utils.js +++ b/lib/source-code/token-store/utils.js @@ -4,20 +4,6 @@ */ "use strict"; -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -/** - * Gets `token.range[0]` from the given token. - * @param {Node|Token|Comment} token The token to get. - * @returns {number} The start location. - * @private - */ -function getStartLocation(token) { - return token.range[0]; -} - //------------------------------------------------------------------------------ // Exports //------------------------------------------------------------------------------ @@ -31,10 +17,18 @@ function getStartLocation(token) { */ exports.search = function search(tokens, location) { for (let minIndex = 0, maxIndex = tokens.length - 1; minIndex <= maxIndex;) { - const index = Math.trunc((minIndex + maxIndex) / 2); + + /* + * Calculate the index in the middle between minIndex and maxIndex. + * `| 0` is used to round a fractional value down to the nearest integer: this is similar to + * using `Math.trunc()` or `Math.floor()`, but performance tests have shown this method to + * be faster. + */ + const index = (minIndex + maxIndex) / 2 | 0; const token = tokens[index]; + const tokenStartLocation = token.range[0]; - if (location <= getStartLocation(token)) { + if (location <= tokenStartLocation) { if (index === minIndex) { return index; }