https://i5lin.top/blog/post/2511378330 #86
Replies: 1 comment 1 reply
-
type Arguments<T> = T extends (...args: infer A) => any ? A : never
type Return<T> = T extends (...args: any[]) => infer R ? R : never
function time<F extends Function>(fn: F, ...args: Arguments<F>): Return<F> {
console.time()
const result = fn(...args)
console.timeEnd()
return result
}
function time<F extends Function>(fn: F, ...args: Arguments<F>): Return<F> {
console.time();
const result = fn(...args);
console.timeEnd();
return result;
}
function add(a: number, b: number): number {
return a + b;
}
const sum = time(add, 1, 2); // 调用 time 函数测量 add 函数的执行时间
console.log(sum);
// 输出:
// [0.09ms] default
// 3 这一块的代码示例中, |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
https://i5lin.top/blog/post/2511378330
A blog about software development and other things
https://i5lin.top/blog/post/2511378330
Beta Was this translation helpful? Give feedback.
All reactions