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

@tzwel/perftime

v1.6.12

Published

A simple and flexible utility for measuring function execution time

Downloads

17

Readme

⏱️ perfTime

GitHub Repo stars

PerfTime is a simple and flexible utility for measuring function execution time. No depenencies.

It works by wrapping the performance.now() method and gives you a simple to use API for quick debugging purposes

Installation:

npm i @tzwel/perftime

Then require it in your project

const perfTime = require('@tzwel/perftime')

Usage:

One-liner

You can pass your function to perfTime and then call the .run() method. This exectues the function, measures its execution time and then logs the result.

new perfTime({function: yourFunctionHere}).run()
new perfTime({function: asyncFunction}).runAsync() // for asynchronous functions

Basic usage

function someRandomFunction() {
	// initialize perfTime and start measuring
	const measurement = new perfTime('function name (or you can leave this blank)').start()

	// code to be measured goes here

	measurement.stop() // stop the measurement and log results
	// => Executing 'someRandomFunction' took 0.006400000000000735ms
}

You can also start the measurement at any point later:

const measurement = new perfTime({function: someRandomFunction})
measurement.start()
// code to be measured goes here
measurement.stop()

The name of the measured function can be set in four different ways;

PerfTime accepts an options object or a string with a name as argument

new perfTime('some function name') // string name
new perfTime({functionName: 'some function name'}) // options object with provided function name
new perfTime({function: someFunction}) // options object with the function name derived automatically from the passed function
new perfTime() // empty, function gets called an *unnamed function*

Multiple measures and average

You can measure a function multiple times and then get its average execution time

[!WARNING] When getting average time, the first result always gets omitted because the first function call is always slower before optimizations take place

It can be done using the one-liner like this:

const measurement = new perfTime({function: someRandomFunction}).run(5) // benchmarks the function 5 times
console.log(measurement.averageTime) // log the average time of execution

// Async variant:
new perfTime({function: asyncFunction}).runAsync(5).then((measurement)=> {
	console.log(measurement.averageTime);
})

Or inside the function like this:

function someRandomFunction() {
	const measurement = new perfTime({function: someRandomFunction})
	
	for (let index = 0; index < 15; index++) { // a for loop that executes code multiple times
		measurement.start()
		// function code to be measured goes here
		measurement.stop()
	}

	console.log(measurement.averageTime);
}

Or by wrapping the function call in a for loop (you can implement this however you want)

const measurement = new perfTime({function: someRandomFunction})
for (let index = 0; index < 15; index++) {
	measurement.start()
	someRandomFunction()
	measurement.stop()
}
console.log(measurement.averageTime);

Async

Currently async functions can only be benchmarked using runAsync and are run one by one, not in parallel