mesqueeb / is-what

JS type check (TypeScript supported) functions like `isPlainObject() isArray()` etc. A simple & small integration.
https://mesqueeb.github.io/is-what/
MIT License
169 stars 18 forks source link

Add benchmarks #39

Open jcbhmr opened 1 year ago

jcbhmr commented 1 year ago

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! πŸ‘

mesqueeb commented 1 year ago

This seems so cool. I'd love to add these benchmarks. The question would be how do we easily add this table to the README? I guess we can just put in a screenshot and manually update it once in a while.