0

I have an array of objects in a web application, the values of which get updated several times. Im trying to work out the best/fastest way

I have a jsPerf here - http://jsperf.com/marker-assignment-test The 'normal' test is how Im currently doing it.

Anyone have any ideas for a faster method?

EDIT: the 'normal' test is how I have it in the web application, but I want to optimise it. The only thing that can not change, is that the objects are stored in an array, and that several values will need updating at the same time

atmd
  • 7,430
  • 2
  • 33
  • 64
  • Are you currently optimizing real code or do you want to know how you should write it in the first place? – Alex Turpin Jan 06 '12 at 15:14
  • edited the question (looking to optimise, with one restriction) – atmd Jan 06 '12 at 15:17
  • Terrible place to look for optimization.. use the first form only if you are getting paid by lines of code. Look for optimization in your (ab)use of jQuery – Esailija Jan 06 '12 at 15:18
  • 1
    abuse? It doesn't use jquery? – atmd Jan 06 '12 at 15:20
  • @DAVIEAC Your web app doesn't use jQuery? Then why did you tag it jQuery ;P What I meant is that this kind of optimization has no effect on the app performance and you should look at actual bottlenecks (Most likely DOM and jQuery related). – Esailija Jan 06 '12 at 15:23
  • sorry no, the web app uses jquery, I added it to the jsPerf incase anyone wanted to suggest a more optimal solution using it. My fault I should have been clearer – atmd Jan 06 '12 at 15:24

1 Answers1

2

Variables are extremely cheap in JavaScript, so you should look at using them. I've cached the result of myArray[1] in a variable, and it's quicker than your tests.

If you have many objects in the array, you should also look at optimizing the iteration of the array; Are loops really faster in reverse?

Community
  • 1
  • 1
Matt
  • 74,352
  • 26
  • 153
  • 180