Initialize the find-related DIACRITICS_EXCEPTION_STR constant lazily

Adding some logging with `console.{time, timeEnd}` around all the constant definitions at the top of the `web/pdf_find_controller.js` file, I noticed that computing `DIACRITICS_EXCEPTION_STR` took close to half the total time.
My first idea was just to try and make it slightly more efficient, by reducing the amount of iterations and intermediate allocations. However, with this constant only being used during "match diacritics" searches it thus seemed like a good candidate for lazy initialization.

*Please note:* Given that this is a micro optimization, I fully understand if the patch is rejected.
This commit is contained in:
Jonas Jenwald 2022-11-15 12:46:16 +01:00
parent 859335a1ae
commit 176e8f0ddc

View File

@ -76,9 +76,7 @@ const DIACRITICS_EXCEPTION = new Set([
// https://www.compart.com/fr/unicode/combining/132
0x0f74,
]);
const DIACRITICS_EXCEPTION_STR = [...DIACRITICS_EXCEPTION.values()]
.map(x => String.fromCharCode(x))
.join("");
let DIACRITICS_EXCEPTION_STR; // Lazily initialized, see below.
const DIACRITICS_REG_EXP = /\p{M}+/gu;
const SPECIAL_CHARS_REG_EXP =
@ -675,6 +673,10 @@ class PDFFindController {
if (matchDiacritics) {
// aX must not match aXY.
if (hasDiacritics) {
DIACRITICS_EXCEPTION_STR ||= String.fromCharCode(
...DIACRITICS_EXCEPTION
);
isUnicode = true;
query = `${query}(?=[${DIACRITICS_EXCEPTION_STR}]|[^\\p{M}]|$)`;
}