perf.js
author Oleksandr Gavenko <gavenkoa@gmail.com>
Sat, 06 Sep 2014 19:51:14 +0300
changeset 5 f1dd66c68f4e
child 57 94e1b2d0bd31
permissions -rw-r--r--
Performance testing toolkit.

"use strict";

////////////////////////////////////////////////////////////////
// Performance testing toolkit.
////////////////////////////////////////////////////////////////


/* Invoce 'fn' function 'n' times with 'this' set to 'ctx'.
 * Use 'ctx' to initialise test and to pass state between calls. */
function perf(msg, fn, n, ctx) {
    if (typeof n !== "number" || n < 1)
        n = 1;
    var tsFrom = new Date().getTime();
    for (i = 0; i < n; i++)
        fn.call(ctx);
    var tsTo = new Date().getTime();
    console.log(msg + ": %f ms, %d u/s", tsTo-tsFrom, i/(tsTo - tsFrom)*1000);
}