Skip to content

Commit

Permalink
feat(timed): add func cost fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellipse120 committed May 15, 2024
1 parent 92f51b4 commit 296ecc9
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion note.md
Original file line number Diff line number Diff line change
Expand Up @@ -4601,4 +4601,31 @@ onBeforeUnmount(() => {
---
## 164.
## 164. calc func runing cost time
```js
function timed(f) {
return function (...args) {
console.log(`Entering function ${f.name}`);
let startTime = Date.now();
try {
return f(...args);
} finally {
console.log(`Exiting ${f.name} after
${Date.now() - startTime}ms`);
}
};
}

function benchmark(n) {
let sum = 0;
for(let i = 1; i <= n; i++) sum += i;

return sum;
}

timed(benchmark)(1000000);
```
---

0 comments on commit 296ecc9

Please sign in to comment.