Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmarks #39

Open
jcbhmr opened this issue May 26, 2023 · 1 comment
Open

Add benchmarks #39

jcbhmr opened this issue May 26, 2023 · 1 comment
Assignees

Comments

@jcbhmr
Copy link
Collaborator

jcbhmr commented May 26, 2023

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
Copy link
Owner

mesqueeb commented Jun 3, 2023

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.

@jcbhmr jcbhmr changed the title Consider adding benchmarks? Add benchmarks Jun 3, 2023
@jcbhmr jcbhmr self-assigned this Jun 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants