window.js
1 // Public: Measure how long a function takes to run. 2 // 3 // description - A {String} description that will be logged to the console when 4 // the function completes. 5 // fn - A {Function} to measure the duration of. 6 // 7 // Returns the value returned by the given function. 8 window.measure = function(description, fn) { 9 let start = Date.now(); 10 let value = fn(); 11 let result = Date.now() - start; 12 console.log(description, result); 13 return value; 14 }; 15 16 // Public: Create a dev tools profile for a function. 17 // 18 // description - A {String} description that will be available in the Profiles 19 // tab of the dev tools. 20 // fn - A {Function} to profile. 21 // 22 // Returns the value returned by the given function. 23 window.profile = function(description, fn) { 24 window.measure(description, function() { 25 console.profile(description); 26 let value = fn(); 27 console.profileEnd(description); 28 return value; 29 }); 30 };