Repository: mozilla/pdf.js
File: web/pdf_find_controller.js (JavaScript)

Code before:
    const flags = `g${isUnicode ? "u" : ""}${caseSensitive ? "" : "i"}`;
    query = new RegExp(query, flags);

    const matches = [];
    let match;
    while ((match = query.exec(pageContent)) !== null) {
      if (
        entireWord &&
        !this.#isEntireWord(pageContent, match.index, match[0].length)
      ) {
        continue;
      }
      matches.push({ index: match.index, length: match[0].length });
    }
    return matches;
  }

  #extractText() {
    // Perform text extraction once if this method is called multiple times.
    if (this._extractTextPromises.length > 0) {
      return;
    }

    let deferred = Promise.resolve();
    const textOptions = { disableNormalization: true };
    const document = this._pdfDocument;
    for (let i = 0, ii = this._linkService.pagesCount; i < ii; i++) {
      const { promise, resolve } = Promise.withResolvers();
      this._extractTextPromises[i] = promise;

      deferred = deferred.then(async () => {
        if (document !== this._pdfDocument) {
          resolve();
          return;
        }
        await document
          .getPage(i + 1)
          .then(pdfPage => pdfPage.getTextContent(textOptions))
          .then(
            textContent => {
              const strBuf = [];

              for (const textItem of textContent.items) {
                strBuf.push(textItem.str);
                if (textItem.hasEOL) {
                  strBuf.push("\n");
                }
              }

              // Store the normalized page content (text items) as one string.
              [

Review comment (suggestion):
Nit: 
```suggestion
    const pdfDoc = this._pdfDocument;
```

Code after:
    const flags = `g${isUnicode ? "u" : ""}${caseSensitive ? "" : "i"}`;
    query = new RegExp(query, flags);

    const matches = [];
    let match;
    while ((match = query.exec(pageContent)) !== null) {
      if (
        entireWord &&
        !this.#isEntireWord(pageContent, match.index, match[0].length)
      ) {
        continue;
      }
      matches.push({ index: match.index, length: match[0].length });
    }
    return matches;
  }

  #extractText() {
    // Perform text extraction once if this method is called multiple times.
    if (this._extractTextPromises.length > 0) {
      return;
    }

    let deferred = Promise.resolve();
    const textOptions = { disableNormalization: true };
    const pdfDoc = this._pdfDocument;
    for (let i = 0, ii = this._linkService.pagesCount; i < ii; i++) {
      const { promise, resolve } = Promise.withResolvers();
      this._extractTextPromises[i] = promise;

      deferred = deferred.then(async () => {
        if (pdfDoc !== this._pdfDocument) {
          resolve();
          return;
        }
        await pdfDoc
          .getPage(i + 1)
          .then(pdfPage => pdfPage.getTextContent(textOptions))
          .then(
            textContent => {
              const strBuf = [];

              for (const textItem of textContent.items) {
                strBuf.push(textItem.str);
                if (textItem.hasEOL) {
                  strBuf.push("\n");
                }
              }

              // Store the normalized page content (text items) as one string.
              [