Skip to content
/ perfTime Public

⏱️ Measure execution time in 1 line of code. A lightweight benchmark utility. No dependencies.

License

Notifications You must be signed in to change notification settings

tzwel/perfTime

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

perfTime

PerfTime is a simple utility for measuring function execution time

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

Usage:

Assign perfTime to a variable and pass an options object containing the function. If omitted, function name is set to unnamed function

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

function someRandomFunction() {
	const measurement = new perfTime({function: someRandomFunction})
	measurement.start()

	// function code to be measured

	measurement.stop()
	// => Executing 'someRandomFunction' took 0.006400000000000735ms
}

someRandomFunction()

Multiple measures and average

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

It can be done inside the function like this (useful when you want to limit the scope of the measurement):

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); // log the average time of execution
}

Or by wrapping the function call in a for loop

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

Compensation

PerfTime subtracts 0.0192 from the result by default. To disable this behavior, set compensation to 0:

const measurement = new perfTime({compensation: 0})

About

⏱️ Measure execution time in 1 line of code. A lightweight benchmark utility. No dependencies.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published