3

I'm trying to make a function that will time other JavaScript functions.

Basically, it would do something like this:

var start = new Date().getTime();

// the function

var end = new Date().getTime();
return end - start;

But, I'd like to be able to call it on any function. For example, I could say timeThisFunction(add) or timeThisFunction(subtract) and it would see how long it took the functions add and subtract to run.

Is this possible in JavaScript? I know there's scoping issues that could be involved. What's the best way to implement it?

2
  • 3
    If you're interested in this for some serious purpose and not just as a learning project, you might want to check out Benchmark.js. Commented Oct 25, 2014 at 16:07
  • 1
    Use performance.now() instead of Date(). It is much more precise. Commented Oct 25, 2014 at 16:28

2 Answers 2

4

If you just want to be able to time a single call to a function, it's pretty easy:

function timer(fn) {
  var start = new Date().getTime();

  fn();

  var end = new Date().getTime();
  return end - start;
}

Passing functions around is no different than passing around any other sort of value.

If you need to time a function that requires parameters, just wrap it in another function:

var millis = timer(function() { theRealFunction(something, 22); });

The extra wrapper function will add a tiny amount of overhead, but it shouldn't matter for most purposes.

Similarly, if you need to time a function that expects this to be bound, you'd bind it before the call:

var millis = timer(someObject.someMethod.bind(someObject));

Now, all that said, if you've got a single JavaScript function call that takes enough time for this mechanism to give meaningful results, then there's probably some serious performance flaw worth investigating.

Sign up to request clarification or add additional context in comments.

3 Comments

Hopefully the function doesn't return anything itself... :P
Yes, but what about those scoping issues? What if fn uses this?
@Juhana then bind it before calling the timer function.
1

Just as an option, you could make timimng function to perform some work for you (context binding, arguments passing).

As a downside, apply is known to be slower then usual function call, so this will no be 100% accurate, but if what is needed is to compare two different implementations of the same function, then it can be useful.

function timeIt(fn, context) {
        var args = Array.prototype.slice.call(arguments, 2),
            start = new Date().getTime();

        fn.apply(context, args);

        return new Date().getTime() - start;

}

Example usage would be:

    function log(what, smthElse) { //ugly function for demonstration
        console.log(what + ' ' +  smthElse);
    };

    timeIt(log, null, 'hello', 'stranger');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.