Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
tinybench
Advanced tools
Readme
Benchmark your code easily with Tinybench, a simple, tiny and light-weight 6KB
benchmarking library!
You can run your benchmarks in multiple JavaScript runtimes, Tinybench is
completely based on the Web APIs with proper timing using process.hrtime
or
performance.now
.
Event
and EventTarget
compatible eventsIn case you need more tiny libraries like tinypool or tinyspy, please consider submitting an RFC
$ npm install -D tinybench
You can start benchmarking by instantiating the Bench
class and adding
benchmark tasks to it.
const bench = new Bench({ time: 100 });
bench
.add("foo", () => {
// code
})
.add("bar", async () => {
// code
});
await bench.run();
The add
method accepts a task name and a task function, so it can benchmark
it! This method returns a reference to the Bench instance, so it's possible to
use it to create an another task for that instance.
Note that the task name should always be unique in an instance, because Tinybench stores the tasks based
on their names in a Map
.
Bench
The Benchmark instance for keeping track of the benchmark tasks and controlling them.
Options:
type Options = {
/**
* time needed for running a benchmark task (milliseconds) @default 500
*/
time?: number;
/**
* number of times that a task should run if even the time option is finished @default 10
*/
iterations?: number;
/**
* function to get the current timestamp in milliseconds
*/
now?: () => number;
/**
* An AbortSignal for aborting the benchmark
*/
signal?: AbortSignal;
/**
* warmup time (milliseconds) @default 100ms
*/
warmupTime?: number;
/**
* warmup iterations @default 5
*/
warmupIterations?: number;
};
async run()
: run the added tasks that were registered using the add
methodasync warmup()
: warm up the benchmark tasksreset()
: reset each task and remove its resultadd(name: string, fn: Fn)
: add a benchmark task to the task mapFn
: () => any | Promise<any>
remove(name: string)
: remove a benchmark task from the task mapget results(): (TaskResult | undefined)[]
: (getter) tasks results as an arrayget tasks(): Task[]
: (getter) tasks as an arraygetTask(name: string): Task | undefined
: get a task based on the nameTask
A class that represents each benchmark task in Tinybench. It keeps track of the results, name, Bench instance, the task function and the number of times the task function has been executed.
constructor(bench: Bench, name: string, fn: Fn)
bench: Bench
name: string
: task namefn: Fn
: the task functionruns: number
: the number of times the task function has been executedresult?: TaskResult
: the result objectasync run()
: run the current task and write the results in Task.result
objectasync warmup()
: warm up the current tasksetResult(result: Partial<TaskResult>)
: change the result object valuesreset()
: reset the task to make the Task.runs
a zero-value and remove the Task.result
objectTaskResult
the benchmark task result object.
type TaskResult = {
/*
* the last error that was thrown while running the task
*/
error?: unknown;
/**
* The amount of time in milliseconds to run the benchmark task (cycle).
*/
totalTime: number;
/**
* the minimum value in the samples
*/
min: number;
/**
* the maximum value in the samples
*/
max: number;
/**
* the number of operations per second
*/
hz: number;
/**
* how long each operation takes (ms)
*/
period: number;
/**
* task samples of each task iteration time (ms)
*/
samples: number[];
/**
* samples mean/average (estimate of the population mean)
*/
mean: number;
/**
* samples variance (estimate of the population variance)
*/
variance: number;
/**
* samples standard deviation (estimate of the population standard deviation)
*/
sd: number;
/**
* standard error of the mean (a.k.a. the standard deviation of the sampling distribution of the sample mean)
*/
sem: number;
/**
* degrees of freedom
*/
df: number;
/**
* critical value of the samples
*/
critical: number;
/**
* margin of error
*/
moe: number;
/**
* relative margin of error
*/
rme: number;
/**
* p75 percentile
*/
p75: number;
/**
* p99 percentile
*/
p99: number;
/**
* p995 percentile
*/
p995: number;
/**
* p999 percentile
*/
p999: number;
};
Events
Both the Task
and Bench
objects extend the EventTarget
object, so you can attach listeners to different types of events
in each class instance using the universal addEventListener
and
removeEventListener
.
/**
* Bench events
*/
export type BenchEvents =
| "abort" // when a signal aborts
| "complete" // when running a benchmark finishes
| "error" // when the benchmark task throws
| "reset" // when the reset function gets called
| "start" // when running the benchmarks gets started
| "warmup" // when the benchmarks start getting warmed up (before start)
| "cycle" // when running each benchmark task gets done (cycle)
| "add" // when a Task gets added to the Bench
| "remove"; // when a Task gets removed to the Bench
/**
* task events
*/
export type TaskEvents =
| "abort"
| "complete"
| "error"
| "reset"
| "start"
| "warmup"
| "cycle";
For instance:
// runs on each benchmark task's cycle
bench.addEventListener("cycle", (e: BenchEvent) => {
const task = e.currentTarget!;
});
// runs only on this benchmark task's cycle
task.addEventListener("cycle", (e: BenchEvent) => {
const task = e.currentTarget!;
});
BenchEvent
type BenchEvent = Event & {
target: Task | null;
currentTarget: Task | null;
};
Mohammad Bagher |
---|
Uzlopak | poyoho |
---|
Feel free to create issues/discussions and then PRs for the project!
FAQs
_I'm transitioning to a full-time open source career. Your support would be greatly appreciated 🙌_ <source media="(prefers-color-scheme: dark)" srcset="https://polar.sh/embed/tiers.svg?org=tinyli
We found that tinybench demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).