Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36
Chrome 59
Mac OS X 10.12.5
Other
7 years ago
Test name Executions per second
Function based 8257.2 Ops/sec
Array based 6471.9 Ops/sec
Script Preparation code:
x
 
function delay(ms, val = true) {
  let timeoutId;
  return new Promise(resolve => {
    timeoutId = setTimeout(() => resolve(val), ms);
  });
}
function runTest(resultFunc) {
    const res = resultFunc();
    for(let i=0; i<20; i++) {
        res.add(delay(i));
    }
    return res.get();
}
Tests:
  • Function based

     
    const createResultPromiseFunc = () => {
      let res = Promise.resolve();
      return {
        get() { return res; },
        add(nextPromise) {
          res = res.then(() => nextPromise);
          return this;
        }
      };
    };
    runTest(createResultPromiseFunc);
  • Array based

     
    const createResultPromiseArray = () => {
      const res = [];
      return {
        get() { return Promise.all(res); },
        add(nextPromise) {
          res.push(nextPromise);
          return this;
        }
      };
    };
    runTest(createResultPromiseArray);