Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

measuring the memory usage, execution time, and performance of a JavaScript function

Iv looked around for a existing lib/solution for what I want but can't find anything, so Im looking to do it myself, and looking for some pointers.

Im looking to create a javascript testing suite, to do a/b testing on two function, so lets just say I have

function foo(){ var x = new Object();}

and

function bar(){var x = {};}

I want to be able to measure the execution time, the memory usage and any other useful stats (like how benchmark can show operations per second)

What ways are there, using javascript, to measure the above

I know a few, but It would be nice to start from fresh and see what else is out there, so all suggestions are very much welcome

thanks

N.B Im not asking for browser plugins or chrome dev tools, the measuring needs to be done in js

like image 280
atmd Avatar asked Sep 22 '26 16:09

atmd


2 Answers

var start = new Date().getTime();
*Your Code*
var end = new Date().getTime(),
    time = end - start,
    execs = 1000 / time;

console.log('time in ms: '+time);
console.log('operations per second: '+execs);

No idea how you would get the memory usage of the execution...

like image 87
Simon Avatar answered Sep 25 '26 07:09

Simon


performance / time: jsperf.com

other profiling - try Chrome's Inspector

like image 26
Guard Avatar answered Sep 25 '26 05:09

Guard