Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ForbesLindesay / throat / 499442415

20 Jan 2021 - 20:02 coverage: 86.364% (-13.6%) from 100.0%
499442415

Pull #57

github

GitHub
Merge ff63aec58 into f8ced5e31
Pull Request #57: perf: do not put closures on the queue

24 of 30 branches covered (80.0%)

56 of 68 new or added lines in 1 file covered. (82.35%)

76 of 88 relevant lines covered (86.36%)

28.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.75
/index.js
1
'use strict';
2

3
function throatInternal(size) {
3×
4
  var queue = new Queue();
34×
5
  var s = size | 0;
34×
6

7
  function run(fn, self, args) {
3×
8
    if ((s | 0) !== 0) {
93×
9
      s = (s | 0) - 1;
58×
10
      return new Promise(function (resolve) {
58×
11
        resolve(fn.apply(self, args));
58×
12
      }).then(onFulfill, onReject);
13
    }
14
    return new Promise(function (resolve) {
35×
15
      queue.push(new Delayed(resolve, fn, self, args));
35×
16
    }).then(runDelayed);
17
  }
18
  function runDelayed(d) {
3×
19
    try {
35×
20
      return Promise.resolve(d.fn.apply(d.self, d.args)).then(
35×
21
        onFulfill,
22
        onReject
23
      );
24
    } catch (ex) {
NEW
25
      onReject(ex);
!
26
    }
27
  }
28
  function onFulfill(result) {
3×
29
    release();
72×
30
    return result;
72×
31
  }
32
  function onReject(error) {
3×
33
    release();
21×
34
    throw error;
21×
35
  }
36
  function release() {
3×
37
    var next = queue.shift();
93×
38
    if (next) {
93×
39
      next.resolve(next);
35×
40
    } else {
41
      s = (s | 0) + 1;
58×
42
    }
43
  }
44

45
  return run;
34×
46
}
47

48
function earlyBound(size, fn) {
3×
49
  const run = throatInternal(size | 0);
20×
50
  return function () {
20×
51
    var args = new Array(arguments.length);
60×
52
    for (var i = 0; i < arguments.length; i++) {
60×
53
      args[i] = arguments[i];
162×
54
    }
55
    return run(fn, this, args);
60×
56
  };
57
}
58
function lateBound(size) {
3×
59
  const run = throatInternal(size | 0);
14×
60
  return function (fn) {
14×
61
    if (typeof fn !== 'function') {
36×
62
      throw new TypeError(
3×
63
        'Expected throat fn to be a function but got ' + typeof fn
64
      );
65
    }
66
    var args = new Array(arguments.length - 1);
33×
67
    for (var i = 1; i < arguments.length; i++) {
33×
68
      args[i - 1] = arguments[i];
9×
69
    }
70
    return run(fn, this, args);
33×
71
  };
72
}
73
module.exports = function throat(size, fn) {
3×
74
  if (typeof size === 'function') {
40×
75
    var temp = fn;
9×
76
    fn = size;
9×
77
    size = temp;
9×
78
  }
79
  if (typeof size !== 'number') {
40×
80
    throw new TypeError(
3×
81
      'Expected throat size to be a number but got ' + typeof size
82
    );
83
  }
84
  if (fn !== undefined && typeof fn !== 'function') {
37×
85
    throw new TypeError(
3×
86
      'Expected throat fn to be a function but got ' + typeof fn
87
    );
88
  }
89
  if (typeof fn === 'function') {
34×
90
    return earlyBound(size | 0, fn);
20×
91
  } else {
92
    return lateBound(size | 0);
14×
93
  }
94
};
95

96
module.exports.default = module.exports;
3×
97

98
function Delayed(resolve, fn, self, args) {
3×
99
  this.resolve = resolve;
35×
100
  this.fn = fn;
35×
101
  this.self = self || null;
35×
102
  this.args = args;
35×
103
}
104

105
var blockSize = 64;
3×
106
function Queue() {
3×
107
  this._s1 = [];
34×
108
  this._s2 = [];
34×
109
  this._shiftBlock = this._pushBlock = new Array(blockSize);
34×
110
  this._pushIndex = 0;
34×
111
  this._shiftIndex = 0;
34×
112
}
113

114
Queue.prototype.push = function (value) {
3×
115
  if (this._pushIndex === blockSize) {
Branches [[10, 0]] missed. 35×
NEW
116
    this._pushIndex = 0;
!
NEW
117
    this._s1[this._s1.length] = this._pushBlock = new Array(blockSize);
!
118
  }
119
  this._pushBlock[this._pushIndex++] = value;
35×
120
};
121

122
Queue.prototype.shift = function () {
3×
123
  if (
93×
124
    this._pushBlock === this._shiftBlock &&
125
    this._pushIndex === this._shiftIndex
126
  ) {
127
    return undefined;
58×
128
  }
129
  if (this._shiftIndex === blockSize) {
Branches [[13, 0]] missed. 35×
NEW
130
    this._shiftIndex = 0;
!
NEW
131
    var s2 = this._s2;
!
NEW
132
    if (s2.length === 0) {
Branches [[14, 0], [14, 1]] missed. !
NEW
133
      var s1 = this._s1;
!
NEW
134
      if (s1.length === 0) {
Branches [[15, 0], [15, 1]] missed. !
NEW
135
        return undefined;
!
136
      }
NEW
137
      this._s1 = s2;
!
NEW
138
      s2 = this._s2 = s1.reverse();
!
139
    }
NEW
140
    this._shiftBlock = s2.pop();
!
141
  }
142
  var result = this._shiftBlock[this._shiftIndex];
35×
143
  this._shiftBlock[this._shiftIndex++] = null;
35×
144
  return result;
35×
145
};
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2023 Coveralls, Inc