In code snippet below the promise is recursively called after completion of itself. I ran the following code snippet on Edge and on Chrome and am seeing a marked difference in execution time. Is there something wrong that I am doing? How I can improve the execution time on Edge?
Please ignore the promise 'anti-pattern'. It is just to simulate an asynchronous method.
Chrome result - Count - 99999 Start 1512416096194 last update 1512416096509 End 1512416096577 recursiveFnReturnsPromise finished
execution time - 397ms
Edge result - Count - 99999 Start 1512415183349 last update 1512415508413 End 1512415907219 recursiveFnReturnsPromise finished
execution time between the start and the end- 723870ms
execution time between last the DOM update and the end- 398806ms
//large array
let aValues = Array(100000);
//utility function
function writeln(text) {
let p = document.createElement('div');
p.innerText = text;
document.getElementById('v1').appendChild(p);
}
writeln('Start ' +Date.now()); //note start recursion time
recursiveFnReturnsPromiseV1(aValues, 1).then(function() {
writeln('End ' +Date.now());//note end recursion time
writeln('recursiveFnReturnsPromise finished');
}, function() {
writeln('End' +Date.now());
writeln('recursiveFnReturnsPromise failed');
})
//the recursive functions which returns a promise
function recursiveFnReturnsPromiseV1(pValues, ix) {
if (pValues.length <= ix)
return Promise.resolve();
return new Promise(function(c, e) {
document.getElementById('output').innerText = ix;
if(ix==99999) writeln('last update ' +Date.now());
c();
}).then(function() {
return recursiveFnReturnsPromiseV1(pValues, ++ix);
})
}
Count - <span id='output'></span>
<div id='v1'></div>
Note - for anyone interested in trying to do promise recursion right please see related post - What is the difference in following pattern to call recursive JavaScript function which returns a promise?