0

I am running the following loop in javascript:

var d =0;
for(var i=0;i<bigarray1.length;i++){
       d += bigarray1[i] - bigarray2[i];
}

bigarray1 and bigarray2 are typed javascript arrays. Are there any perf optimizations specific to typed arrays that I can do (eg using reduce etc)

Captain Jack sparrow
  • 959
  • 1
  • 13
  • 28
  • Seems the only optimizations you can do here is how you are defining your typed arrays as data structures. You may want to take a look at [this question](http://stackoverflow.com/questions/24853686/javascript-typedarray-performance). – Spencer Wieczorek Jul 06 '16 at 20:29
  • `reduce` won't be faster, but it would be more readable for a functional programmer. – 4castle Jul 06 '16 at 20:33
  • cache the length too - so that on every iteration you aren't calling that method. – james emanon Jul 06 '16 at 20:48
  • 1
    `[].reduce()` would be a lot slower than a tight loop like that, not to mention that fact that it doesn't run on typedArrays and would therefore require expensive conversion... – dandavis Jul 06 '16 at 20:53
  • I didn't expected it to be faster using reduce too but I was trying my luck after seeing an answer to this: http://stackoverflow.com/questions/3448347/how-to-scale-an-imagedata-in-html-canvas Look at the answer from user3079576 where he claims that he got a 30x speed bump due to using subarray instead of looping over. This was in the typed array of type uint8clampedarray – Captain Jack sparrow Jul 06 '16 at 21:02

0 Answers0