# All files / lib/util make-measure.js

100% Statements 55/55
90% Branches 9/10
100% Functions 2/2
100% Lines 55/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    552x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    184x
    184x
    184x
    184x
    184x
    184x
    184x
    184x
    184x
    7x
    7x
    7x
    7x
    7x
    7x
    2x
    2x
    5x
    5x
    2x
    2x
    7x
    7x
    7x
    7x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    7x
    7x
    7x
    7x
    184x
    184ximport getConfig from "./config";
    import { VTree } from "./types";

    const prefix = 'diffHTML';
    const marks = new Map();
    const nop = () => {};
    let count = 0;

    /**
     * Creates a measure function that will collect data about the currently running
     * transaction.
     *
     * @param {import('../transaction').default} transaction
     * @return {(name: string) => void}
     */
    export default function makeMeasure(transaction) {
      const { mount, input } = transaction;
      const inputAsVTree = /** @type {VTree} */ (input);
      const id = count++;

      // Marks will only be available if the user has requested they want to collect
      // metrics.
      if (!getConfig('collectMetrics', false)) { return nop; }

      return name => {
        name = `[${id}] ${name}`;

        const { host } = /** @type {any} */ (mount);

        // Use the Web Component name if it's available.
        if (mount && host) {
          name = `${host.constructor.name} ${name}`;
        }
        // Otherwise try an find the function name used.
        else if (inputAsVTree && typeof inputAsVTree.rawNodeName === 'function') {
          name = `${inputAsVTree.rawNodeName.name} ${name}`;
        }

        const endName = `${name}-end`;

        if (marks.has(name)) {
          const prevMark = marks.get(name) || 0;
          const totalMs = (performance.now() - prevMark).toFixed(3);

          marks.delete(name);

          performance.mark(endName);
          performance.measure(`${prefix} ${name} (${totalMs}ms)`, name, endName);
        }
        else {
          marks.set(name, performance.now());
          performance.mark(name);
        }
      };
    }