extended promise with stack trace (v2)

Revision 2 of this benchmark created on


Setup

// Keep a native baseline
const NativePromise = window.Promise;

// Your subclass
class FAKEPROMISE extends Promise {
  constructor(executor) {
    super(executor);
    // Capturing a stack is intentionally expensive—this is what you want to measure
    this.__creationPoint = new Error();
  }
}
const FakePromise = FAKEPROMISE;

class FAKEPROMISEWITHSUPER extends Promise {
  constructor(executor) {
    super(executor);
  }
}
const FakePromiseWithSuper = FAKEPROMISEWITHSUPER;

class FAKEPROMISENOSUPER extends Promise {}
const FakePromiseNoSuper = FAKEPROMISENOSUPER;

// Helper: create N promises of constructor form and resolve them immediately
function batchConstructor(P, n, done) {
  let remaining = n;
  for (let i = 0; i < n; i++) {
    new P(res => res(i)).then(() => {
      if (--remaining === 0) done();
    });
  }
}

// Helper: create N promises of resolve+then form
function batchResolveThen(P, n, done) {
  let remaining = n;
  for (let i = 0; i < n; i++) {
    P.resolve(i).then(() => {
      if (--remaining === 0) done();
    });
  }
}

const N = 1000; // adjust if runs take too long/short

Test runner

Ready to run.

Testing in
TestOps/sec
Native (constructor form)
batchConstructor(NativePromise, N, function () {
  deferred.resolve();
});
ready
Fake (constructor form)
batchConstructor(FakePromise, N, function () {
  deferred.resolve();
});
ready
Native (Promise.resolve().then)
batchResolveThen(NativePromise, N, function () {
  deferred.resolve();
});
ready
Fake (Promise.resolve().then)
batchResolveThen(FakePromise, N, function () {
  deferred.resolve();
});
ready
Native Promise.all
const arr = [];
for (let i = 0; i < N; i++) arr.push(new NativePromise(r => r(i)));
NativePromise.all(arr).then(() => deferred.resolve());
ready
Fake Promise.all
const arr = [];
for (let i = 0; i < N; i++) arr.push(new FakePromise(r => r(i)));
FakePromise.all(arr).then(() => deferred.resolve());
ready
Super only
batchConstructor(FakePromiseWithSuper, N, function () {
  deferred.resolve();
});
ready
No super / bare extends
batchConstructor(FakePromiseNoSuper, N, function () {
  deferred.resolve();
});
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.