Skip to content

Instantly share code, notes, and snippets.

@cevek
Created December 27, 2016 20:42
Show Gist options
  • Save cevek/e60b3ac6d7dc8e54bba9cd1afc79736e to your computer and use it in GitHub Desktop.
Save cevek/e60b3ac6d7dc8e54bba9cd1afc79736e to your computer and use it in GitHub Desktop.
// results: new P, new P ~160ms
// results: new P, new PP ~190ms
// results: new PP, new PP ~210ms
function P() {
}
P.prototype.then = function (fn, arg) {
fn();
};
var Then = P.prototype.then;
P.prototype.resolve = function (promise) {
if (typeof promise === 'object' && promise !== null && typeof promise.then === 'function') {
if (promise.then === Then) {
promise.then(promise.next, promise);
} else {
this.bind(promise);
}
}
};
function bind(promise, _this) {
promise.then(function () {
_this.next();
});
}
P.prototype.bind = function(promise) {
var _this = this;
promise.then(function () {
_this.next();
});
}
P.prototype.next = function () {
};
function PP() {
}
PP.prototype.then = function () {
};
function DD() {
}
var a = [
1,
null,
void 0,
"12",
true,
{},
{x: 1},
{y: 1},
{z: 1},
{m: 1},
{o: 1},
new DD(),
// new P(),
new P(),
new PP(),
// new PP(),
// {then: 1},
// {then: "1"},
// {then: {}},
/*{
then: function () {
}
},
{
then: function () {
}
},*/
// new P()
0
];
var promise = new P();
function abc() {
console.time('perf');
for (var i = 0; i < 1000000; i++) {
for (var j = 0; j < a.length; j++) {
var x = a[j];
promise.resolve(x);
}
}
console.timeEnd('perf');
}
abc();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment