Skip to content

Instantly share code, notes, and snippets.

@briancavalier
Created December 11, 2014 13:38
Show Gist options
  • Select an option

  • Save briancavalier/414de30060f1748176b6 to your computer and use it in GitHub Desktop.

Select an option

Save briancavalier/414de30060f1748176b6 to your computer and use it in GitHub Desktop.
Test program for iterable support in when.all & when/es6-shim/Promise.all
var when = require('when');
function ArrayIterable(a) {
this.array = a;
}
ArrayIterable.prototype._es6shim_iterator_ = function() {
return new ArrayIterator(this.array);
};
function ArrayIterator(a) {
this.array = a;
this.index = 0;
}
ArrayIterator.prototype.next = function() {
if(this.index < this.array.length) {
var i = this.index;
this.index += 1;
return new Iteration(this.array[i]);
}
return Iteration.DONE;
};
function Iteration(value) {
this.value = value;
this.done = false;
}
var DONE = new Iteration(void 0);
DONE.done = true;
Iteration.DONE = DONE;
var a = [];
for(var i=0; i<100000; ++i) {
a.push(when.resolve(i).delay(1));
}
function inc(x) {
return x+1;
}
when.all(a).then(function(a) {
var start = Date.now();
return when.all(a).then(function(a) {
console.log('Array all', Date.now() - start, a.length);
});
}).then(function() {
return when.map(a, inc).then(function(a) {
var start = Date.now();
return when.map(a, inc).then(function(a) {
console.log('Array map', Date.now() - start, a.length);
});
});
}).then(function() {
return when.all(new ArrayIterable(a)).then(function(a) {
var start = Date.now();
return when.all(new ArrayIterable(a)).then(function(a) {
console.log('Iterable all', Date.now() - start, a.length);
});
}).then(function() {
return when.map(new ArrayIterable(a), inc).then(function(a) {
var start = Date.now();
return when.map(new ArrayIterable(a), inc).then(function(a) {
console.log('Iterable map', Date.now() - start, a.length);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment