Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unroll simple loop abstractions #1253

Merged
merged 2 commits into from
Jul 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions lib/internal/queue.js
Original file line number Diff line number Diff line change
@@ -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';
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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