Skip to content

Add benchmarks #39

@jcbhmr

Description

@jcbhmr

Something like https://github.com/tinylibs/tinybench#usage to get a feel for how fast calling these functions (with their recursive delegation and type guards) is compared to just typeof thing === "object" || typeof thing === "function" inline in your JS code. Basically a quick "how much tax?" answer via a benchmark.

something like:

// test/index.bench.ts
import { Bench } from "tinybench";
import { getType, isAnyObject, isPlainObject } from "is-what"; // ../src/index

let i = 0;
const objects = [{}, "", new Number(43), null, globalThis];
const bench = new Bench({ time: 100 });

bench.add("getType()", () => getType(objects[(i = i + (1 % 5))]));
bench.add("inline toString.call().slice()", () =>
  Object.prototype.toString.call(objects[(i = i + (1 % 5))]).slice(8, -1)
);

bench.add("isPlainObject()", () => isPlainObject(objects[(i = i + (1 % 5))]));
bench.add(
  "inline 2x __proto__",
  () => objects[(i = i + (1 % 5))]?.__proto__?.__proto__ == null
);

bench.add("isAnyObject()", () => isAnyObject(objects[(i = i + (1 % 5))]));
bench.add("inline typeof", () => {
  const o = objects[(i = i + (1 % 5))];
  return o && (typeof o === "object" || typeof o === "function");
});

await bench.run();
console.table(bench.table());

image

👆 Showing off how LOW TAX this library is would be a good idea IMO! 👍

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions