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)

5
  • 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. Commented Jul 6, 2016 at 20:29
  • reduce won't be faster, but it would be more readable for a functional programmer. Commented Jul 6, 2016 at 20:33
  • cache the length too - so that on every iteration you aren't calling that method. Commented Jul 6, 2016 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... Commented Jul 6, 2016 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: stackoverflow.com/questions/3448347/… 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 Commented Jul 6, 2016 at 21:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.