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

Tiny benchmarking harness #2699

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions benchmarks/splitDashedKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-disable no-console */
import { Bench } from 'tinybench';

import { splitDashedKey, splitDashedKeyIterative, splitDashedKeyRegex } from 'src/utils/splitDashedKey';

const inputs: string[] = ['mycomponent-0-1', 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6-100-200'];

const bench = new Bench({
name: 'splitDashedKey benchmark',
time: 100,
setup: (_task, mode) => {
// Run the garbage collector before warmup at each cycle
if (mode === 'warmup' && typeof globalThis.gc === 'function') {
globalThis.gc();
}
},
});

for (const input of inputs) {
bench
.add(`[${input}] original`, () => splitDashedKey(input))
.add(`[${input}] regex`, () => splitDashedKeyRegex(input))
.add(`[${input}] iterative`, () => splitDashedKeyIterative(input));
}

bench.run().then(() => {
const table = bench.table();

console.log(bench.name);
console.table(table);
});
15 changes: 15 additions & 0 deletions benchmarks/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"include": ["*.ts"],
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"strict": true,
"esModuleInterop": true,
"moduleResolution": "Bundler",
"noEmit": true,
"noUncheckedIndexedAccess": true,
"paths": {
"src/*": ["../src/*"],
},
}
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"tsc": "yarn gen && tsc && tsc --project test/tsconfig.json",
"tsc:watch": "yarn gen && tsc --watch",
"tsc:watch:cypress": "yarn gen && tsc --watch --project test/tsconfig.json",
"lint": "yarn gen && eslint ."
"lint": "yarn gen && eslint .",
"bench": "tsx --expose_gc --tsconfig benchmarks/tsconfig.json benchmarks/${0}"
},
"devDependencies": {
"@babel/core": "7.25.8",
Expand Down Expand Up @@ -112,10 +113,12 @@
"source-map-loader": "5.0.0",
"style-loader": "4.0.0",
"terser-webpack-plugin": "5.3.10",
"tinybench": "^3.0.3",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into it, and while tinybench seems to work well enough for the example use-case here, it doesn't seem there's any straight-forward ways to benchmark react components (including re-rendering) with it. There are libraries to do that, some of which seems to spin up headless chrome to run their benchmark in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In principle I can't think of any reason why a React test renderer wouldn't run under NodeJS in this context when it is no problem in a jest testrunner? If it's practically much harder for some reason I'm not opposed to other tools at all, and I think we can even have multiple tools for different use-cases (though I have previously often just stuck to the same benchmarking harness for all level of benchmarks previously). But I think we should start to define a process around this to figure out

  • What should we require benchmarks for
  • How should we track the numbers over time

"ts-jest": "29.2.5",
"ts-loader": "9.5.1",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"tsx": "^4.19.2",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 🤩 We already have ts-node as a typescript runner, but I looked into it and tsx looks a lot better/more promising. I'll look into replacing our few usages of ts-node with tsx after this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oo didn't even notice, used tsx because it was part of the example code of tinybench

"typescript-plugin-css-modules": "^5.1.0",
"use-immer": "^0.10.0",
"utility-types": "3.11.0",
Expand Down
66 changes: 66 additions & 0 deletions src/utils/splitDashedKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function splitDashedKey(componentId: string): SplitKey {
depth: [],
};
}

// If all you want is the baseId, use this instead
// If you see this, feel free to optimize this further ;)
export function getBaseComponentId(componentId: string): string {
Expand All @@ -64,3 +65,68 @@ export function getBaseComponentId(componentId: string): string {

return componentId;
}

const regex = /(\p{L}+|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})-(\d+)-(\d+)/u;
export function splitDashedKeyRegex(componentId: string): SplitKey {
const result = componentId.match(regex);
if (!result || result.length < 4) {
return {
baseComponentId: componentId,
stringDepth: '',
stringDepthWithLeadingDash: '',
depth: [],
};
}

const stringDepth = `${result[2]}-${result[3]}`;
return {
baseComponentId: result[1],
stringDepth,
stringDepthWithLeadingDash: `-${stringDepth}`,
depth: [parseInt(result[2], 10), parseInt(result[3], 10)],
};
}

export function splitDashedKeyIterative(componentId: string): SplitKey {
let firstDepth: number | undefined;
let secondDepth: number | undefined;
let lastDashIndex = componentId.length;
for (let i = componentId.length - 1; i >= 0; i--) {
if (componentId[i] === '-') {
lastDashIndex = i;

if (secondDepth === undefined) {
secondDepth = parseInt(componentId.slice(i + 1), 10);
if (Number.isNaN(secondDepth)) {
throw new Error('Invalid componentId');
}
} else {
firstDepth = parseInt(componentId.slice(i + 1, lastDashIndex), 10);
if (Number.isNaN(secondDepth)) {
throw new Error('Invalid componentId');
}
if (i - 1 < 0) {
throw new Error('Invalid componentId');
}
break;
}
}
}

if (firstDepth === undefined || secondDepth === undefined) {
return {
baseComponentId: componentId,
stringDepth: '',
stringDepthWithLeadingDash: '',
depth: [],
};
}

const stringDepth = `${firstDepth}-${secondDepth}`;
return {
baseComponentId: componentId.slice(0, lastDashIndex),
stringDepth,
stringDepthWithLeadingDash: `-${stringDepth}`,
depth: [firstDepth, secondDepth],
};
}
Loading
Loading