-
-
Notifications
You must be signed in to change notification settings - Fork 909
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
324 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { bench } from "vitest"; | ||
import { mergeProps, splitProps } from "../src"; | ||
import * as old from "./component.old" | ||
|
||
const createObject = (prefix: string, amount: number, descriptor: any) => { | ||
const proto: Record<string, any> = {} | ||
for (let index = 0; index < amount; ++index) | ||
proto[`${prefix}${index}`] = descriptor | ||
return Object.defineProperties({}, proto) | ||
} | ||
|
||
function pickProps<T extends number>(inProps: Record<string, any>, ...amounts: T[]): { | ||
[K in T]: { names: string[], props: Record<string, any> } | ||
} { | ||
const result: Record<number, { names: string[], props: Record<string, any> }> = {} | ||
const desc = Object.getOwnPropertyDescriptors(inProps) | ||
const inPropNames = Object.keys(inProps) | ||
for (const amount of amounts) { | ||
const names = inPropNames.slice(0, amount) | ||
const props: Record<string, any> = {} | ||
for (const name of inPropNames.slice(0, amount)) { | ||
Object.defineProperty(props, name, desc[name]) | ||
} | ||
result[amount] = { names, props } | ||
} | ||
return result | ||
} | ||
|
||
const mergePropsItems: { | ||
name: string | ||
func: Function | ||
}[] = [{ | ||
name: "mergeProps", | ||
func: mergeProps | ||
}, { | ||
name: "oldMergeProps", | ||
func: old.mergeProps | ||
}] | ||
|
||
const splitPropsItems: { | ||
name: string | ||
func: Function | ||
}[] = [{ | ||
name: "splitProps", | ||
func: splitProps | ||
}, { | ||
name: "oldSplitProps", | ||
func: old.splitProps | ||
}] | ||
|
||
|
||
const staticDesc = { value: 1, writable: true, configurable: true, enumerable: true } | ||
const signalDesc = { get() { return 1 }, configurable: true, enumerable: true } | ||
const staticObject = createObject("signal", 100, signalDesc) | ||
const staticProps = pickProps(staticObject, 0, 5, 15, 25, 50, 100) | ||
const iterations = 1000 | ||
const inputs = [ | ||
[0, 15], | ||
[0, 100], | ||
[25, 100], | ||
[50, 100], | ||
[100, 25] | ||
] as const | ||
|
||
for (const input of inputs) { | ||
const [a, b] = input | ||
describe(`splitProps(${a}, ${b})`, () => { | ||
for (const { name, func } of splitPropsItems) | ||
bench(`${name}(${a}, ${b})`, () => { | ||
func(staticProps[a].props, staticProps[b].names) | ||
}, { | ||
iterations | ||
}) | ||
}); | ||
describe(`splitProps(${a}, ${b}, ${b})`, () => { | ||
for (const { name, func } of splitPropsItems) | ||
bench(`${name}(${a}, ${b}, ${b})`, () => { | ||
func(staticProps[a].props, staticProps[b].names, staticProps[b].names) | ||
}, { | ||
iterations | ||
}) | ||
}); | ||
describe(`mergeProps(${a}, ${b})`, () => { | ||
for (const { name, func } of mergePropsItems) | ||
bench(`${name}(${a}, ${b})`, () => { | ||
func(staticProps[a].props, staticProps[b].props) | ||
}, { | ||
iterations: 1000 | ||
}) | ||
}) | ||
} | ||
|
Oops, something went wrong.