|
| 1 | +// time action with separate start and finish callbacks |
| 2 | +// for example |
| 3 | +/* |
| 4 | + worker.onmessage = function (e) { |
| 5 | + console.log('worker has finished'); |
| 6 | + renderPrimes(e.data); |
| 7 | + }; |
| 8 | + document.addEventListener('DOMContentLoaded', function() { |
| 9 | + document.querySelector('#find').addEventListener('click', function () { |
| 10 | + var n = Number(document.querySelector('#n').value); |
| 11 | + primesApp.findFirstPrimes(n); |
| 12 | + }); |
| 13 | + }); |
| 14 | +
|
| 15 | + you can profile as |
| 16 | +
|
| 17 | + timeSeparateCallback(primesApp, 'findFirstPrimes', worker, 'onmessage'); |
| 18 | +*/ |
| 19 | +(function timeSeparateCallback(obj1, methodName1, obj2, methodName2) { |
| 20 | + 'use strict'; |
| 21 | + |
| 22 | + console.assert(obj1, 'missing first object'); |
| 23 | + console.assert(obj2, 'missing second object'); |
| 24 | + |
| 25 | + var m1 = obj1[methodName1]; |
| 26 | + console.assert(typeof m1 === 'function', 'cannot find first method ' + methodName1); |
| 27 | + var m2 = obj2[methodName2]; |
| 28 | + console.assert(typeof m2 === 'function', 'cannot find first method ' + methodName2); |
| 29 | + |
| 30 | + obj1[methodName1] = function () { |
| 31 | + console.profile('separate'); |
| 32 | + console.time('separate'); |
| 33 | + m1.apply(obj1, arguments); |
| 34 | + }; |
| 35 | + |
| 36 | + obj2[methodName2] = function () { |
| 37 | + console.timeEnd('separate'); |
| 38 | + console.profileEnd('separate'); |
| 39 | + m2.apply(obj2, arguments); |
| 40 | + }; |
| 41 | + |
| 42 | + console.log('wrapped', methodName1, 'and', methodName2, 'in timing calls'); |
| 43 | + |
| 44 | + // modify the arguments below |
| 45 | +}(window, 'postMessage', window, 'onmessage')); |
0 commit comments