JavaScript design using two arrays v.s. one object array, the pron and corn?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


JavaScript design using two arrays v.s. one object array, the pron and corn?



Let us consider performance, design, readable, what is the better design for JavaScript? Question is that keep a key value table!



design one:


key= ['A', 'B', 'C', 'D']
value = [1, 2, 3, 4]



design two:


map = {'A':1, 'B':2, 'C':3, 'D':4]



design three


map = [ ['A', 1], ['B', 2], ['C', 3], ['D', 4]]



design four:


data = [{key:'A', value:1}, {key:'B', value:2}, {key:'C', value:3},{key:'D', value:4}]



My colleague say 'design one' is the best, because it keep simple, no redundant object, easy to access by index, and have best performance because JavaScript access array is faster than access Map.



However, I think that we need to put relationship data together in order to reach
Encapsulation. I don't known how more time be waste if using Map? Is it really very slow than use Array?



How do you think?





"access array faster than map" .. that is silly. Create a perf test and test both yourselves
– charlietfl
27 mins ago







Two is the normal way. The others are strange. Consider a JavaScript array is basically a map keyed by ints anyway.
– Nathan Hughes
27 mins ago







First, that's not a Map, is a key-value pair object instead. The best approach it depends on the current design. Btw, this is a primarily opinion based question!
– Ele
25 mins ago





There is technically no right or wrong answer to this question because we don't have the context in which you will be using it. Your colleague is wrong because you should never optimise things like this at the start of a project and you can't optimise without benchmarks. Pick the one that makes your data easier for you to work with.
– Emett Speer
13 mins ago









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived