All files / lib/util memory.js

100% Statements 56/56
100% Branches 9/9
100% Functions 3/3
100% Lines 56/56

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 55 56 577x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1586x 1586x 1586x 561x 719x 719x 561x 1586x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1150x 396x 509x 509x 396x 1150x 1150x 1150x 1150x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 688x 7828x 7828x 7828x 688x 688x  
import Pool from './pool';
import { NodeCache, VTree } from './types';
 
const { protect, unprotect, memory } = Pool;
 
/**
 * Ensures that vTree is not recycled during a render cycle.
 *
 * @param {VTree} vTree
 * @return {void}
 */
export function protectVTree(vTree) {
  protect(vTree);
 
  if (vTree.childNodes.length) {
    for (let i = 0; i < vTree.childNodes.length; i++) {
      protectVTree(vTree.childNodes[i]);
    }
  }
}
 
/**
 * Recycles a VTree by unprotecting itself, removing its DOM Node reference, and
 * recursively unprotecting all nested children. Resets the VTree's attributes
 * and childNode properties afterwards, as these can contribute to unwanted
 * increases in the heap.
 *
 * @param {VTree} vTree
 * @return {void}
 */
export function unprotectVTree(vTree) {
  if (vTree.childNodes.length) {
    for (let i = 0; i < vTree.childNodes.length; i++) {
      unprotectVTree(vTree.childNodes[i]);
    }
  }
 
  NodeCache.delete(vTree);
  unprotect(vTree);
}
 
/**
 * Collects any unused VTree's and puts them back into the free Set. This is
 * primarily used by tests, but could also be useful for specific niche cases
 * as a way to ease memory/CPU pressure when lots of temporary trees are
 * created but never used.
 *
 * @return {void}
 */
export function gc() {
  memory.allocated.forEach(vTree => {
    memory.free.add(vTree);
    memory.allocated.delete(vTree);
    NodeCache.delete(vTree);
  });
}