3

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

4 Answers 4

2
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...

Sign up to request clarification or add additional context in comments.

3 Comments

great stuff. I wonder if this is more accurate then using console.time()? Anyone know?
It's pretty hard to say whether console.time() with console.timeEnd() or my approach is more accurate, in my test they always have a maximum of 1ms difference
just looked at some of the benchmark.js stuff and they seem to use the new Date() method
1

performance / time: jsperf.com

other profiling - try Chrome's Inspector

3 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
jsperf.com doesn't help comparing 2 JS functions performance? funny
The comment below is an automatic comment left when reviewing a "low quality" post. Yours looks like a comment, so it should be transformed into one. If you don't want your answers to be flagged as low quality the next time you can try adding some more explanations or example code.
0

Another option (for execution timing), if you are using/can use Node.js is process.hrtime()

See this link for details/example:

Measurement with process.hrtime()

And for memory usage:

Sizeof.js

Comments

0

For memory usage maybe you can use something like window.performance.memory like this:

console.log(performance.memory.usedJSHeapSize);

The result is in bytes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.