1.0.4 • Published 5 years ago

@jusenpai/timer v1.0.4

Weekly downloads
-
License
ISC
Repository
github
Last release
5 years ago

Timer

A lightweight library capable of measuring how much time a callback requires to execute.

Installation

npm install --save @jusenpai/timer

Usage

It works with synchronous functions:

const timer = require('@jusenpai/timer');

function sum(x, y) {
    return x + y;
}

timer(sum, 10, 12)
    .then(result => {
        // amount of time required to sum the numbers
        console.log(result.time);
        
        // the value returned by the function
        console.log(result.value);
    });

It also works with promises:

const timer = require('@jusenpai/timer');

function fetchData() {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve({data: 'Hello'});
        }, 500);
    });
}

timer(fetchData)
    .then(result => {
        // will wait for the timeout, so it will be over 500 ms
        console.log(result.time); 
        
        // will be our fetched data
        console.log(result.value);
    });