median times in miliseconds for 20000 reps of a simple counting algorithm, testing various forms of dispatch, eg closures, classes, goog.inherit, dojo.hitch (method.apply), dojo.delegate, dojo.extend, a few of my methods similar to hitch, inline, custom function as a proxy, and proxies using method.call instead of method.apply
caveat emptor: benchmarking is hard, and i'm new enough to javascript that i'm not 100% confident in the numbers. my tests are very simple, but i have tried to make them difficult to optimize out. the basic algorithm is the following, which is far from foolproof.
function(ko) {
ko = ++ko % this.po;
ko == 0 && (this.po++);
caveat emptor: benchmarking is hard, and i'm new enough to javascript that i'm not 100% confident in the numbers. my tests are very simple, but i have tried to make them difficult to optimize out. the basic algorithm is the following, which is far from foolproof.
function(ko) {
ko = ++ko % this.po;
ko == 0 && (this.po++);
return ko;
}
median | std dev | |||||
Chrome | Firefox 3.5 | IE 7 | Chrome | Firefox 3.5 | IE 7 | |
class | 2 | 14 | 62 | 0.41 | 0.85 | 0.5 |
goog.inherit | 2 | 16 | 78 | 0.73 | 0.9 | 0.35 |
closure | 2 | 16 | 63 | 0.5 | 0.62 | 0.5 |
dojo.delegate | 3 | 15 | 63 | 0.52 | 0.7 | 4.03 |
dojo.extend | 3 | 14 | 63 | 0.62 | 0.64 | 0.45 |
dojo.hitch | 13 | 141 | 297 | 1.25 | 190.2 | 7.93 |
nqo.proxy | 4 | 136 | 281 | 0.41 | 139.61 | 0.5 |
hitchArgs | 256 | 381 | 1312 | 3.33 | 189.79 | 7.18 |
proxyArgs | 202 | 273 | 813 | 5.58 | 190.16 | 6.43 |
functionProxy | 3 | 24 | 125 | 0.26 | 0.88 | 0 |
functionArgs | 3 | 24 | 125 | 0.41 | 0.9 | 4.12 |
callProxy | 6 | 35 | 156 | 0.45 | 1.08 | 0 |
callProxy1 | 3 | 29 | 141 | 0.35 | 1.42 | 5.44 |
callArgs | 25 | 40 | 172 | 0.72 | 1.55 | 0.41 |
inline | 1 | 5 | 15* | 0.41 | 0.26 | 5.43 |
Conclusions: Chrome > Firefox > IE 7, function.call > function.apply, any of the class or closure based approaches worked about the same, regardless of the inheritance mechanism. the dojo.extend style seems slightly faster to invoke, but for any real problem i don't think it's worth worrying about. dojo.hitch and function.apply seem massively slow, especially when combining arguments from the hitch and the invocation. function.call is much faster, and a custom function as a proxy is faster yet - taking about twice as long as directly accessing the source object
* my tool reported mostly 0 msec on IE 7, but every by-hand test that i did was 15 or 16 msec. pretty sure that the delta in Date() isn't working in tight loops
No comments:
Post a Comment