New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@weedzcokie/benchmark

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@weedzcokie/benchmark

Tiny benchmarking utility

  • 0.4.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

benchmark-ts

Example:

import { Benchmark } from "@weedzcokie/benchmark";

function cellNumberToName(x: number) {
    let name = "";
    while (x >= 0) {
        name = String.fromCodePoint(x % 26 + 65) + name;
        x = Math.floor(x / 26) - 1;
    }
    return name;
}

const DATA_SIZE = 100000;

const lookups: string[] = [];
for (let i = 0; i < DATA_SIZE; ++i) {
    lookups.push(cellNumberToName(i));
}

function mapAssignment() {
    const map = new Map();
    for (const x of lookups) {
        map.set(x, true);
    }
    return map;
}
function setAssignment() {
    const set = new Set();
    for (const x of lookups) {
        set.add(x);
    }
    return set;
}
function objectAssignment() {
    const object: Record<string, boolean> = {};
    for (const x of lookups) {
        object[x] = true;
    }
    return object;
}

const bench = new Benchmark();
bench.add("Map assignment", mapAssignment);
bench.add("Set assignment", setAssignment);
bench.add("Object assignment", objectAssignment);

await bench.run();

console.table(bench.table());

Output:

┌───────────────────┬────────────┬────────────┬──────────┬────────────────┬────────────────┬───────────────────┐
│      (index)      │ 99th (µs)  │  +/- (µs)  │   Op/s   │ Set assignment │ Map assignment │ Object assignment │
├───────────────────┼────────────┼────────────┼──────────┼────────────────┼────────────────┼───────────────────┤
│  Set assignment   │ '5455.871' │ '1761.586' │ '143.26' │      '-'       │     '1.12'     │      '1.44'       │
│  Map assignment   │ '6651.903' │ '1131.191' │ '127.92' │     '0.89'     │      '-'       │      '1.29'       │
│ Object assignment │ '7741.439' │ '2270.702' │ '99.36'  │     '0.69'     │     '0.78'     │        '-'        │
└───────────────────┴────────────┴────────────┴──────────┴────────────────┴────────────────┴───────────────────┘

Output

We do not print anything. You can listen on the task-start event to know when a task is starting and task-done to know then a task is done.

Example:

bench.on("task-start", task => {
    console.log(`Running task '${task.label}'...`);
});

bench.on("task-done", (task, result) => {
    console.log("Task done:", task.label, result);
});

Events

Typescript everywhere so probably easiest to look at type definitions.

task-start

Fires when a task starts running.

task-done

Fires when a task is done running.

done

Fires when all tasks are done running.

Keywords

FAQs

Package last updated on 10 Aug 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc