All files / lib/internal freelist.js

100% Statements 30/30
100% Branches 8/8
100% Functions 3/3
100% Lines 30/30

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 3112x 12x 12x 12x 12x 12x 12x 12x 1055x 1055x 1055x 1055x 1055x 12x 12x 6192x 6192x 6192x 6192x 12x 12x 6164x 6161x 6161x 6161x 3x 6164x 12x 12x 12x  
'use strict';
 
const {
  ReflectApply,
} = primordials;
 
class FreeList {
  constructor(name, max, ctor) {
    this.name = name;
    this.ctor = ctor;
    this.max = max;
    this.list = [];
  }
 
  alloc() {
    return this.list.length > 0 ?
      this.list.pop() :
      ReflectApply(this.ctor, this, arguments);
  }
 
  free(obj) {
    if (this.list.length < this.max) {
      this.list.push(obj);
      return true;
    }
    return false;
  }
}
 
module.exports = FreeList;