1

This is a slightly similar question to javascript switch vs loop on array, but different. Compare

function Function1 (n) {
  switch (n) {
    case 0:
      return "apple";
    break;
    case 1:
      return "pear";
    break;
    // and a few more, or many more
  }
}

and

var Array2 = ["apple", "pear"]; // and more

which you then call or refer to as needed. The second is much neater, but are there any important disadvantages to it, not just in terms of speed, but also memory use? Will the answer change a lot if there are more cases / elements in the array? Is your answer Javascript-specific or a general tenet of programming? Thank you.

3
  • An Array lookup is the same as an Object lookup, which (in most implementations) is done with a hash table so it's very fast even with large structures. This is different to switch which is effectively the same as a series of ifs
    – Paul S.
    Feb 20, 2014 at 18:35
  • If you need to access data on an index use a structure accesses data on an index. Why jump through multiple conditionals or make the compiler optimize to a lookup and obfuscate the code? Feb 20, 2014 at 18:36
  • Thanks. No reason except not knowing if there weren't any hidden advantages to it. :) Feb 20, 2014 at 18:40

2 Answers 2

0

I will answer as a general tenet of programming:

(1) Arrays store all the Strings in memory for their entire scope, or in a language such as C/C++ where memory is manually allocated, until the memory is explicitly freed. For large arrays, this can be very memory-intensive.

(2) Unlike for switch(), where adding an object occurs in constant time, adding or removing an object in the beginning or the middle of an array will require all the subsequent elements to be recopied to their new places. It may require all the objects to be recopied if the array needs to grow, since array as a data structure has a fixed number of elements and growing the array will require the declaration and reinitialization of the elements of a new array if there is no space left.

(3) For lookup, switch() occurs in linear time, whereas the array lookup occurs in much faster constant time.

3
  • 1
    I don't know that the switch has an advantage over the array in terms of memory usage. Code takes up space too. The strings referenced in the switch cases have to be stored as well.
    – Nate C-K
    Feb 20, 2014 at 19:02
  • Good point. The arrays, though, store the strings in consecutive locations in memory which could be problematic if the array is large. Storage of switch cases, on the other hand, depends on the compiler. Feb 20, 2014 at 19:06
  • If your data is getting THAT large you're probably better off just putting it in a database anyway.
    – Nate C-K
    Feb 20, 2014 at 23:40
0

A while back, I created this jsperf to compare switch with other methods. Switch is very slow in chrome:

http://jsperf.com/bmcgin-switch-vs-if-vs-obj

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.