diff options
author | Graeme Yeates <yeatesgraeme@gmail.com> | 2016-07-29 14:23:38 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-29 14:23:38 -0400 |
commit | 60dc3b658e4c6bdc8a2469e278a474e1586fa74a (patch) | |
tree | 310feb7f3d756b0ff858cd7a892709e4c0b6e038 /lib | |
parent | 5c523fd05fc390eaae72145950814bd686ccba71 (diff) | |
parent | 5b8931d40205778a5bf4e936efb10046b79eb018 (diff) | |
download | async-60dc3b658e4c6bdc8a2469e278a474e1586fa74a.tar.gz |
Merge pull request #1253 from caolan/loops
Unroll simple loop abstractions
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/queue.js | 27 | ||||
-rw-r--r-- | lib/priorityQueue.js | 7 | ||||
-rw-r--r-- | lib/race.js | 7 |
3 files changed, 19 insertions, 22 deletions
diff --git a/lib/internal/queue.js b/lib/internal/queue.js index 5b5883d..067aaf6 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/_baseRest'; @@ -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); + } } |