paint-brush
Javascript: Performance measurement of functionsby@peterchang_82818
1,543 reads
1,543 reads

Javascript: Performance measurement of functions

by August 7th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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.
featured image - Javascript: Performance measurement of functions
 HackerNoon profile picture

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://github.com/wahengchang/javascript-function-performance-measurement


$ npm run install$ node example

Reference

https://github.com/wahengchang/javascript-function-performance-measurement/blob/master/README.md