I'm sure there are about 6,427 bajillion ways to do this, but here is a simple JavaScript function profiler that I found helpful when pinpointing some performance bottlenecks. The cool thing about this tool is that the profiling is specific to exactly the places in the code that you're interested in, meaning you don't have to sift through profile information for a bunch of functions you don't care about.
Start each function you want to profile with
profile_tool.start("my function name or whatever", "starting");
and end with
profile_tool.log(true)
which will produce a profile time for the entire function:
. finish bar,61,1421458778841,1421458778902
This line tells us that the total runtime for bar is 61 ms (the label is independent of the function and can be whatever you want at whatever granularity you want).
Within a function, additional calls to log
profile.log("is that previous line my problem?")
will be reported relative to the previous call to log() or start():
.. not much happening yet bar,0,1421458778841,1421458778841 .. did some work bar,61,1421458778841,1421458778902
Here we can see that the expensive operation within bar() is whatever appears between these two call to log.
Requires Underscore, because I'm lazy. If anyone really cares, I can remove that requirement.
Typical usage
Include the JavaScript in your page
Wire up your functions
function foo() { profile_tool.start("foo", "starting"); // blah profile_tool.log("did some work"); // blah profile_tool.log("did some more work"); // blah profile_tool.log(true) }
Copy the results to the clipboard: in the console:
> copy(profile_tool.as_csv())
Paste into your favorite spreadsheet or dataframe