ActiveDen

Interesting results when comparing Arrays and Objects

5010 posts The Dude Abides
  • United States
  • Elite Author
  • Has been a member for 4-5 years
  • Exclusive Author
  • Sold between 50 000 and 100 000 dollars
  • Contributed a Tutorial to a Tuts+ Site
  • Author had a Free File of the Month
+4 more
CodingJack says
http://jsperf.com/globals-vs-local-vars

Disregard the title of that link. The test originally started as something else and then evolved.

Probably need to do more tests but from that sample it looks like Arrays are faster for instances when you need to store both a key and a value. Certainly Objects are prettier/better organized, so deciding to use the Array approach probably depends on what you’re doing.

In the Array example, an additional 10k Arrays are created for each loop compared to the Object example. So essentially, 2[] < 1{}

5010 posts The Dude Abides
  • United States
  • Elite Author
  • Has been a member for 4-5 years
  • Exclusive Author
  • Sold between 50 000 and 100 000 dollars
  • Contributed a Tutorial to a Tuts+ Site
  • Author had a Free File of the Month
+4 more
CodingJack says
http://jsperf.com/globals-vs-local-vars/3

small edit to the Object loop (wasn’t actually reading the property’s value before)

319 posts
  • Author had a File in an Envato Bundle
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Sold between 1 000 and 5 000 dollars
  • United States
jwmcpeak says

Arrays are designed for enumeration; they’ll always be faster than objects when it comes to that. But you also have to factor in ease-of-use. Yes, you can use a two element array to contain a key/value pair, but that stinks for an API .

by
by
by
by
by