Skip to content

Instantly share code, notes, and snippets.

@yadimon
Last active April 26, 2016 11:36
Show Gist options
  • Save yadimon/c68e807d96760c713ba35300ba92e423 to your computer and use it in GitHub Desktop.
Save yadimon/c68e807d96760c713ba35300ba92e423 to your computer and use it in GitHub Desktop.
measure execution time of function (mini benchmark) in promises in node.js
//perf start
var lastCallName = '';
function timePromise(name) {
var lastNameToEnd = lastCallName ? lastCallName : '';
lastCallName = name;
return function (data) {
if (lastNameToEnd) console.timeEnd(lastNameToEnd);
console.time(name);
return data;
};
}
//perf end
//your code
do1Promise()
.then(do2Promise)
.then(function(result){return do3Promise(result+1)});
//your code with time of execution
Promise.resolve()
.then(timePromise('do1Promise'))
.then(function(){return do1Promise()})
.then(timePromise('do2Promise'))
.then(do2Promise)
.then(timePromise('do3Promise'))
.then(function(result){return do3Promise(result+1)})
.then(timePromise('end it all')); //todo fix end call
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment