> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/func/kind/higher/decorator/benchmark-f.md).

# benchmark(f)

[JS](/web/js.md) ⟩ [technique](/web/js/tech.md) ⟩ [decorator](/web/js/val/func/kind/higher/decorator.md) ⟩ :floppy\_disk: benchmark(f)

{% hint style="success" %}
([decorator](/web/js/val/func/kind/higher/decorator.md))&#x20;

{% endhint %}

{% tabs %}
{% tab title="💾 程式" %}

* replit：[timed(f)](https://replit.com/@pegasusroe/timedf#index.js)

```javascript
// ⭐ benchmark a function
function benchmark(f) {

    return function(...args) {

        console.log(`• Entering function: ${f.name}(${args}) ...`);
        let startTime = Date.now();

        try {
            return f.apply(this, args);    // forward call
        } finally {
            const s = (Date.now() - startTime) / 1000;
            console.log(`• Exiting ${f.name} after ${s} sec.`);
        }

    };
}
```

example

```javascript
// test function
function addFrom1To(n) {
    let sum = 0;
    for (let i = 1; i <= n; i++) sum += i;
    return sum;
}

benchmark(addFrom1To)(1000000)                // 500000500000
// • Entering function: addFrom1To(1000000) ...
// • Exiting addFrom1To after 0.023 sec.
```

{% endtab %}

{% tab title="📗 參考" %}

* [x] [JavaScript: The Definitive Guide](/web/master/ref/javascript-the-definitive-guide.md) > 8.3.4 The Spread Operator for Function Calls
  {% endtab %}

{% tab title="👥 相關" %}

* [measureTime()](/web/js/async/async/measuretime.md)
  {% endtab %}
  {% endtabs %}
