2
function reverseArray(array) {
  var output = [];
  for (var i = array.length - 1; i >= 0; i--)
    output.push(array[i]);
  return output;
}

function reverseArrayInPlace(array) {
  for (var i = 0; i < Math.floor(array.length / 2); i++) {
    var old = array[i];
    array[i] = array[array.length - 1 - i];
    array[array.length - 1 - i] = old;
  }
  return array;
}

I tried performance.now() on chrome browser but getting different results every time. Also, is performance.now() good way to check the performance of code?

kamal kokne
  • 175
  • 10
  • 2
    Looks like a job for [jsperf](http://jsperf.com) – Pointy Apr 25 '16 at 13:39
  • 1
    @Pointy jsperf is not available. – Dmitri Pavlutin Apr 25 '16 at 13:42
  • Oh well that'd be a pretty big problem I guess :) – Pointy Apr 25 '16 at 13:43
  • The functions do different things. One constructs a new array from the given array without changing the existing, the second functions manipulates the existing array. You cannot use them interchangeably. – Max Sorin Apr 25 '16 at 13:46
  • Can anyone please answer why performance.now() giving different output on same machine same browser? – kamal kokne Apr 25 '16 at 13:49
  • @kamalkokne Are the same processes running during your tests? There are many things which can influence your test. – A1rPun Apr 25 '16 at 13:52
  • @A1rPun Yes. Same processes. First output: 0.1250 milliseconds for first function, 0.2950 milliseconds for 2nd function. Second output: 0.1850 milliseconds for 1st function, 0.1200 milliseconds for 2nd function. – kamal kokne Apr 25 '16 at 13:57

2 Answers2

3

Your second function is faster.

Think about how many times the loop needs to 'loop' - in the second function you cut the loop in half so it should perform better. While in the first function you loop all the way through the array.

What we are talking about here is Time Complexity, of which there are some great answers on SO, this one in particular: How to find time complexity of an algorithm

Community
  • 1
  • 1
omarjmh
  • 13,632
  • 6
  • 34
  • 42
  • Can you please answer why performance.now() giving different output on same machine same browser? – kamal kokne Apr 25 '16 at 13:49
  • what are the differences? and you should use jsperf...when its back up of course – omarjmh Apr 25 '16 at 13:51
  • 0.1250 milliseconds for first function, 0.2950 milliseconds for 2nd function. Another result: 0.1850 milliseconds for 1st function, 0.1200 milliseconds for 2nd function. – kamal kokne Apr 25 '16 at 13:55
  • there really are too many factors that could be in play to give you a definitive answer... sorry – omarjmh Apr 25 '16 at 13:59
0

In the second function you are aplying what is called loop unroll. Is a known method to increase performance (during a loop, instructions loaded into a segmented pipeline get droped when the loop completes an iteration, thus having to load them again).

However, this is not always true (see this post Is GCC loop unrolling flag really effective? )

The best you can do is benchmark it by yourself.

Community
  • 1
  • 1
Nadir
  • 1,799
  • 12
  • 20
  • Can you please answer why performance.now() giving different output on same machine same browser? – kamal kokne Apr 25 '16 at 13:46
  • @kamalkokne Google chrome has built-in performance measurement tools. I'd recommend to use that one instead https://developer.chrome.com/devtools/docs/cpu-profiling – Nadir Apr 25 '16 at 13:52