Skip to content

Commit

Permalink
Merge pull request #1253 from caolan/loops
Browse files Browse the repository at this point in the history
Unroll simple loop abstractions
  • Loading branch information
megawac committed Jul 29, 2016
2 parents 5c523fd + 5b8931d commit 60dc3b6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 22 deletions.
27 changes: 13 additions & 14 deletions 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';
Expand All @@ -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
};

Expand All @@ -40,29 +41,27 @@ export default function queue(worker, concurrency, payload) {
} else {
q._tasks.push(item);
}

});
}
setImmediate(q.process);
}

function _next(tasks) {
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();
Expand Down
7 changes: 3 additions & 4 deletions lib/priorityQueue.js
@@ -1,4 +1,3 @@
import arrayEach from 'lodash/_arrayEach';
import isArray from 'lodash/isArray';
import noop from 'lodash/noop';

Expand Down Expand Up @@ -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
};
Expand All @@ -69,7 +68,7 @@ export default function(worker, concurrency) {
} else {
q._tasks.push(item);
}
});
}
setImmediate(q.process);
};

Expand Down
7 changes: 3 additions & 4 deletions 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';

Expand Down Expand Up @@ -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);
}
}
37 changes: 37 additions & 0 deletions perf/suites.js
@@ -1,5 +1,6 @@
var _ = require("lodash");
var tasks;
var count;

module.exports = [{
name: "each",
Expand Down Expand Up @@ -93,6 +94,42 @@ module.exports = [{
async.setImmediate(cb);
}, done);
}
}, {
name: "filter",
args: [
[10],
[300],
[10000]
],
setup: function(c) {
count = c;
tasks = _.range(count);
},
fn: function(async, done) {
async.filter(tasks, function(num, cb) {
async.setImmediate(function() {
cb(null, num > (count / 2));
});
}, done);
}
}, {
name: "filterLimit",
args: [
[10],
[300],
[10000]
],
setup: function(c) {
count = c;
tasks = _.range(count);
},
fn: function(async, done) {
async.filterLimit(tasks, 10, function(num, cb) {
async.setImmediate(function() {
cb(null, num > (count / 2));
});
}, done);
}
}, {
name: "eachOf",
// args lists are passed to the setup function
Expand Down

0 comments on commit 60dc3b6

Please sign in to comment.