Hi All,
I'd like to ask some advice on implementing a basic profiler in
Javascript.
Background: while messing around with cryptography and javascript, I
ended up writing a Javascript library for 128 bit integer operations
(xor, add, mul, etc) as a by-product. I'm now working on benchmarking
and profiling that library.
The Chrome profiler is great, as I'm sure Firebug's must be, but what
I would like is a browser-independent, pure-javascript profiling
framework which I can easily add to my tests. It doesn't have to be
sophisticated, just tell me how much time is spent in each function.
Given my naive understanding of the language and runtime
environment(s), I came up with the following:
1 - recursively walk through the package I want to benchmark,
injecting a rudimentary wrapper with a start/stop timer for each
function
2 - run the benchmark (for now, it's just the test suite)
3 - create a report from the data collected by the wrapper code
(the below code shows exactly how this is done)
It works, but there's a number of problems with it, mainly:
a) the results are accurate only for leaf-functions
b) my wrapper code appears to slow things down quite a bit
What would a better/lighter-weight/less-intrusive way of implementing
a javascript profiler be?
Any hints would be much appreciated.
Many thanks,
Luigi
-- for reference, part of ''profiling framework''
profiler.add = function(target) {
for(var elm in target) {
if (typeof target[elm] == "function" && target[elm].__noprofile
==
undefined) {
target[elm] = profiler.wrap(target, elm)
target[elm].__timespent = 0
}
if (target.propertyIsEnumerable(elm)) {
profiler.add(target[elm])
}
}
}
profiler.wrap = function(obj, prop) {
var oldf = obj[prop]
return function() {
var t_start = new Date().getTime();
var retval = oldf(arguments[0], arguments[1], arguments[2],
arguments[3]) // 4 for now
obj[prop].__timespent += new Date().getTime() - t_start
return retval
}
}
// to profile:
profiler.add(faultylabs.u128)
library.runBenchmark()
profiler.report(faultylabs.u128, document.getElementById('rptdiv'))
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]