Reputation: 1465
I'm building (with a partner) a small web app. It is an information app and pulls the info from a JSON file; we'll end up with about 150 items in the JSON, each with 130 properties.
We build the buttons in the app by querying the JSON, which is stored in localStorage, and getting things like item[i].name and item[i].cssClass to construct buttons.
Question is - would it be more efficient and worth it to create 2 arrays in localStorage that hold the name and cssClass for the purposes of constructing these buttons, or is this a waste of time and should we just pull the name and cssClass straight from the JSON?
I should clarify - We need to the items by sortable by name, cssClass, etc; users can sort the data into lists and the buttons can be constructed as alphabetic lists (which take you to the details of the items) or as category buttons which take you to lists of items in that category.
The issue is - does sorting the JSON carry a significant overhead compared to just sorting the array of names?
Upvotes: 0
Views: 101
Reputation: 22166
Retrieving the i-th element of an array, same as getting a property of an object, has time complexity O(1)
. JSON is quite a high-level language. As long as you don't work with millions of items, you shouldn't care about the implementation details of the interpreter.
I'd guess that the web browser will sure spend much more time working with DOM / rendering the page, compared to operations performed on your data structures.
Upvotes: 2
Reputation: 5459
Is this a waste of time and should we just pull the name and cssClass straight from the JSON?
Yes, it will be fine.
"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%." - http://c2.com/cgi/wiki?PrematureOptimization
Upvotes: 2