0.0.1 • Published 8 years ago
time-func v0.0.1
time-func
time-func
is a higher-order function abstraction used to measure the execution time of synchronous or asynchronous functions, for Node and the browser.
Installation
$ npm install time-func
API
Synchronous function, synchronous API
const time = require('time-func');
// In the synchronous API, the return value is a 2-element array where the first value is the
// original return value of the function, and the second value is the duration of the function in
// milliseconds.
const result = time(() => {
doWorkThatTakes100Milliseconds();
return 5;
});
console.log(result[0]); // 5
console.log(result[1]); // 100
Asynchronous function, asynchronous API
const request = require('request');
const time = require('time-func');
// In the asynchronous API, the first parameter to `time` is a single-parameter function whose
// parameter is invoked as a callback when the asynchronous work is completed. The second parameter
// to `time` is a callback function invoked with two parameters: the "return value" (parameter
// passed to the `done` callback), and the function duration in milliseconds.
time((done) => request.get(..., (err, resp, body) => done(body)), (retVal, duration) => {
console.log(retVal); // body from request
console.log(duration); // duration of request
});
Synchronous function, asynchronous API
You may optionally use the asynchronous callback API for timing synchronous functions.
const time = require('time-func');
time(() => {
doWorkThatTakes100Milliseconds();
return 5;
}, (retVal, duration) => {
console.log(retVal); // undefined
console.log(duration); // 100
});
0.0.1
8 years ago