Good question, Dean. This was presented to me this weekend by someone who needs to work with the data as is. Not sure why but that's what they have to work with.
So regardless of the structure, what *I'm* personally interested in at this moment is helping them optimize the traversal of the data. Can you help? Rey... On Mon, Sep 19, 2011 at 4:15 PM, Dean Landolt <[email protected]> wrote: > > > On Sun, Sep 18, 2011 at 12:19 AM, Rey Bango <[email protected]> wrote: > >> Hey guys, >> >> The code below loops over some JSON data. It' simple in that it looks for >> a specific note and then a specific chord within the note. It then returns >> the strings. >> >> I'm using for loops to handle it but wanted to ask if there was a better, >> faster or more efficient way to do it. >> >> Thoughts? >> >> Rey... >> >> >> function getChord( myNotes, theNote, theChord ) { >> var theChords = ''; >> >> for (var i=0; i < myNotes.length; i++) { >> if (myNotes[i].note == theNote) { >> theChords = myNotes[i].chords; >> for (var i=0; i < theChords.length; i++) { >> if (theChords[i].chord == theChord) { >> return theChords[i].strings; >> }; >> >> }; >> >> }; >> >> }; >> >> } >> >> >> var notes= [{ >> "note": "A", >> "chords": [ >> { >> "chord": "maj7", >> "strings": { >> "1": 0, >> "2": 3, >> "3": 2, >> "4": 0, >> "5": 1, >> "6": 2 >> } >> > > > Just one aside I have to ask -- what's up with this? Is this just a > contrived example? Otherwise all `strings` keys screams out to be an array: > [ 0, 3, 2, 0, 1, 2 ]. > > >> }, >> { >> "chord": "min", >> "strings": { >> "1": 0, >> "2": 3, >> "3": 2, >> "4": 0, >> "5": 1, >> "6": 2 >> } >> } >> ] >> },{ >> "note": "B", >> "chords": [ >> { >> "chord": "maj7", >> "strings": { >> "1": 0, >> "2": 3, >> "3": 2, >> "4": 0, >> "5": 1, >> "6": 2 >> } >> }, >> { >> "chord": "min", >> "strings": { >> "1": 0, >> "2": 3, >> "3": 2, >> "4": 0, >> "5": 1, >> "6": 2 >> } >> } >> ] >> }]; >> >> >> chordStrings = getChord( notes, "A", "maj7" ); >> > > > Assuming you know your data structure schema and which fields you're going > to want to query for you can pretty easily just build some indexing to keep > you from having to walk so much of your data structure every time. A notes > index -- nothing more than an array like this: [ "A","B" ] -- would wipe out > getChord's first for loop which, depending on the use case, could be quite a > gain. Of course in this domain we know it's limited to a total of 12 values > so this optimization is practically useless. Instead we need an index on > both note and chord, a prospect substantially complicated by the choice of > data structure... > > Which is a more important point: why the choice of data structure above? > Nesting chords arrays into a notes array is a lot more painful than just an > array of chords objects, each containing a note property. The only thing the > data structure above buys us is a (slight) space savings by avoiding > duplication of the notes property in every chord. If instead `notes` were an > object keyed by the actual note we'd get the same deduplication benefits as > the current data structure (the `note` doesn't have to be repeated in every > chord) as well as a built-in index (we can efficiently seek to all chords of > a specific note). > > Of course, this still seems crazy to me -- why hack the hell out of your > data structures for optimizations that are either premature or possible in > cleaner ways? The only sane data structure will always be as flat as > possible: an array of `chord` objects that contain their note, their chord, > their strings struck, and anything else. This kind of datastructure can more > easily be validated, indexed and queried in various ways -- either in a > database on the server, locally in IndexedDB, or even just in memory. Or in > all of those places, queried using the same language: "note=A&chord=min" is > what it would look like in RQL [1]. This kind of stuff is possible today, > but not as easy as it should be. > > [1] https://github.com/kriszyp/rql > > > -- > To view archived discussions from the original JSMentors Mailman list: > http://www.mail-archive.com/[email protected]/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/[email protected]/ > > To unsubscribe from this group, send email to > [email protected] > -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
