summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGraeme Yeates <yeatesgraeme@gmail.com>2016-07-23 11:13:22 -0400
committerGraeme Yeates <yeatesgraeme@gmail.com>2016-07-23 11:13:22 -0400
commit66d6bbe7f174bc3adebf71d23d43d6bf05a64fb3 (patch)
tree8e50c6279bfdcb672efebba31d173994bdf925a6
parent4cdeecf56e42ec5150e453e5cf93c89439b15463 (diff)
downloadasync-66d6bbe7f174bc3adebf71d23d43d6bf05a64fb3.tar.gz
Unroll simple loop abstractions
-rw-r--r--lib/internal/queue.js27
-rw-r--r--lib/priorityQueue.js7
-rw-r--r--lib/race.js7
3 files changed, 19 insertions, 22 deletions
diff --git a/lib/internal/queue.js b/lib/internal/queue.js
index 41d6c90..d318ba0 100644
--- a/lib/internal/queue.js
+++ b/lib/internal/queue.js
@@ -1,4 +1,4 @@
-import arrayEach from 'lodash/_arrayEach';
+import indexOf from 'lodash/_baseIndexOf';
import isArray from 'lodash/isArray';
import noop from 'lodash/noop';
import rest from 'lodash/rest';
@@ -23,15 +23,16 @@ export default function queue(worker, concurrency, payload) {
if (!isArray(data)) {
data = [data];
}
- if(data.length === 0 && q.idle()) {
+ if (data.length === 0 && q.idle()) {
// call drain immediately if there are no tasks
return setImmediate(function() {
q.drain();
});
}
- arrayEach(data, function(task) {
+
+ for (var i = 0, l = data.length; i < l; i++) {
var item = {
- data: task,
+ data: data[i],
callback: callback || noop
};
@@ -40,8 +41,7 @@ export default function queue(worker, concurrency, payload) {
} else {
q._tasks.push(item);
}
-
- });
+ }
setImmediate(q.process);
}
@@ -49,20 +49,19 @@ export default function queue(worker, concurrency, payload) {
return rest(function(args){
workers -= 1;
- arrayEach(tasks, function (task) {
- arrayEach(workersList, function (worker, index) {
- if (worker === task) {
- workersList.splice(index, 1);
- return false;
- }
- });
+ for (var i = 0, l = tasks.length; i < l; i++) {
+ var task = tasks[i];
+ var index = indexOf(workersList, task, 0);
+ if (index >= 0) {
+ workersList.splice(index)
+ }
task.callback.apply(task, args);
if (args[0] != null) {
q.error(args[0], task.data);
}
- });
+ }
if (workers <= (q.concurrency - q.buffer) ) {
q.unsaturated();
diff --git a/lib/priorityQueue.js b/lib/priorityQueue.js
index d8b6016..0af0157 100644
--- a/lib/priorityQueue.js
+++ b/lib/priorityQueue.js
@@ -1,4 +1,3 @@
-import arrayEach from 'lodash/_arrayEach';
import isArray from 'lodash/isArray';
import noop from 'lodash/noop';
@@ -57,9 +56,9 @@ export default function(worker, concurrency) {
nextNode = nextNode.next;
}
- arrayEach(data, function(task) {
+ for (var i = 0, l = data.length; i < l; i++) {
var item = {
- data: task,
+ data: data[i],
priority: priority,
callback: callback
};
@@ -69,7 +68,7 @@ export default function(worker, concurrency) {
} else {
q._tasks.push(item);
}
- });
+ }
setImmediate(q.process);
};
diff --git a/lib/race.js b/lib/race.js
index 55d37ee..6937627 100644
--- a/lib/race.js
+++ b/lib/race.js
@@ -1,5 +1,4 @@
import isArray from 'lodash/isArray';
-import arrayEach from 'lodash/_arrayEach';
import noop from 'lodash/noop';
import once from './internal/once';
@@ -44,7 +43,7 @@ export default function race(tasks, callback) {
callback = once(callback || noop);
if (!isArray(tasks)) return callback(new TypeError('First argument to race must be an array of functions'));
if (!tasks.length) return callback();
- arrayEach(tasks, function (task) {
- task(callback);
- });
+ for (var i = 0, l = tasks.length; i < l; i++) {
+ tasks[i](callback);
+ }
}