npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

timely

v0.2.1

Published

Lightweight and easy to use timing decorators

Downloads

689

Readme

Timely - Lightweight timing of JavaScript functions

When developing in JavaScript you often find yourself in situations where you want to measure the time it takes for a function to execute. Profilers can find this out but if you just want to measure the time for a function or two you might want to try Timely. Timely doesn't affect the parameters or return values of the functions that it measures. Instead it decorates the existing functions with a timing functionality that is completely transparent to the callers.

Installation

Node

npm install timely

Browser

bower install timely

Usage (in Node)

var timely = require('timely'),

// Synchronous function (Fibonacci)
fib = function(n) {
	if (n <= 2) return n;
	return fib(n - 1) + fib(n - 2);
},

// Asynchronous function with callback
wait = function(n, cb) {
	setTimeout(function() {
		cb(n);
	}, 100);
},

// Asynchronous function returning a promise
wait2 = function(n) {
  var defer = when.defer();
	setTimeout(function() {
		defer.resolve(n);
	}, 100);
  return defer.promise;
},

// Create a timed verion of the Synchronous funciton
fibT = timely(fib),
// Create a timed verion of the Asynchronous function with callback
waitT = timely.async(wait),
// Create a timed verion of the Asynchronous function returning a promise
waitT2 = timely.promise(wait2),

// Result of Synchronous function
resultSync;

// Call Synchronous function
resultSync = fibT(35);
//Output results of Synchronous function. fibT.time contains the time used for the last call
console.log('fib(35) = ' + resultSync + ', time: ' + fibT.time + 'ms');

// Call Asynchronous function with callback
waitT(42, function(resultAsync) {
	//Output results of Asynchronous function. waitT.time contains the time used for the last call
	console.log('wait(42) = ' + resultAsync + ', time: ' + waitT.time + 'ms');
});

// Call Asynchronous function returning a promise
waitT2(42).then(function(resultAsync) {
	//Output results of Asynchronous function. waitT2.time contains the time used for the last call
	console.log('wait2(42) = ' + resultAsync + ', time: ' + waitT2.time + 'ms');
});

Outputs:

fib(35) = 14930352, time: 201ms
wait(42) = 42, time: 101ms
wait2(42) = 42, time: 102ms

Demo

To run Node demo:

node demo.js

To run Browser demo:

Open demo.html in your favorite browser

API

Functions

Returns a decorated version of a synchronous function func that measures its execution time. After the function has returned the measured time is available in the returned function's property "time". The mean time of all calls is founcd in the property "meanTime".

Arguments

func     {Function} Function to be timed

Returns a decorated version of an asynchronous function func that measures its execution time. After the callback has been called the measured time is available in the returned function's property "time". It is assumed that the callback is the last parameter of the function func. The mean time of all calls is founcd in the property "meanTime".

Arguments

func     {Function} Function to be timed

Returns a decorated version of an asynchronous function func that measures its execution time. After the returned promise has been resolved the measured time is available in the returned function's property "time". The mean time of all calls is founcd in the property "meanTime".

Arguments

func     {Function} Function to be timed

Tests

npm install
npm test

License

(MIT License)

Copyright (c) 2014 Aron Kornhall [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.