0

I've seen multiple times people talking about O(1), O(n) etc in questions but nobody tells what it means and how it works. I tried to search it online but I don't know what keywords to use..

What does these mean and how it applies to situations?

Please consider using these two simple examples and my questions in the spirit of this question.


Object:

  1. Time / performace difference for each level inward?
  2. Difference between .object vs ['object'] in the concept of my question?

pets.dogs or pets['dogs']

pets.dogs.collie or pets['dogs']['collie']

pets.dogs.collie.size or pets['dogs']['collie']['size']


  1. How does array affect this bunch (if I know the key)?

pets.dogs.collie.color[2] and pets.dogs.collie.popularnames[56]


  1. How does array affect this bunch (if I don't know the key and this is in for loop)?

pets.dogs.collie.color[i] and pets.dogs.collie.popularnames[i]


var pets = {

    dogs: {
        chihuahua: { 
            color: ["black", "white"],
            size: "tiny",
            hunting: false,
            popularnames: [ /*100 names*/ ]
        },
        collie: { 
            color: ["brown", "white"],
            size: "medium",
            hunting: false,
            popularnames: [ /*100 names*/ ]
        },
        // etc
    },
    cats: {
        bengal: { 
            color: ["gray", "white"],
            size: "medium",
            fur: "short",
            popularnames: [ /*100 names*/ ]
        },
        birman: { 
            color: ["black", "white"],
            size: "medium",
            fur: "long",
            popularnames: [ /*100 names*/ ]
        },
        // etc
    }
}

Bottom line question:

  1. Should I avoid complicated objects (e.g many, many selectors) and use multiple simpler ones?
  2. Is array iteration performance worse if I iterate array deep / 3-4 levels inside the object?

Array:

  1. Time / performace difference for each level inward (If I know the keys)?

pets[0]

pets[0][1]

pets[0][1][3]


  1. How does object affect this bunch (if I know the key)?

pets[0][1][1]['somekey']


var myArray = [

    0: [
        0: [
            0: [ /* 100 objects */ ],
            1: [ /* 100 objects */ ],
            2: [ /* 100 ints */ ],
            3: [ /* 100 ints */ ]
        ],
        1: [
            0: [ /* 100 objects */ ],
            1: [ /* 100 objects */ ],
            2: [ /* 100 ints */ ],
            3: [ /* 100 ints */ ]
        ],
        // etc
    ],
    1: [
        0: [
            0: [ /* 100 objects */ ],
            1: [ /* 100 objects */ ],
            2: [ /* 100 ints */ ],
            3: [ /* 100 ints */ ]
        ],
        1: [ 
            0: [ /* 100 objects */ ],
            1: [ /* 100 objects */ ],
            2: [ /* 100 ints */ ],
            3: [ /* 100 ints */ ]
        ],
        // etc
    ]
]

Bottom line question:

  1. Should I avoid complicated arrays (e.g many, many levels) and use multiple simpler ones?

Solo
  • 6,687
  • 7
  • 35
  • 67
  • 2
    You have 9 questions within your question? – krillgar Jan 21 '16 at 19:53
  • 1
    Look up "big o notation" – mmccaff Jan 21 '16 at 19:54
  • @krillgar I don't need an answer for each of those questions if you don't feel like answering them. I added these just to describe the situation and help you to answer my question. – Solo Jan 21 '16 at 19:55
  • https://en.wikipedia.org/wiki/Big_O_notation – Matt Burland Jan 21 '16 at 20:02
  • 1
    None of your examples really touch on Big O notation at all. The `.` and `[]` accessor are both `O(1)`. – Matt Burland Jan 21 '16 at 20:05
  • 2
    Possible duplicate of [Plain English explanation of Big O](http://stackoverflow.com/questions/487258/plain-english-explanation-of-big-o) – Matt Burland Jan 21 '16 at 20:05
  • @MattBurland You just said how it touches that. :) That was my main concern: Difference between single accessor and multiple accessors in the spirit of Big O so that I could decide how build my arrays and objects. – Solo Jan 21 '16 at 20:09

1 Answers1

2

For an in-depth explanation of of your question regarding O(1),O(n), please see this wikipedia article on Big_O_notation.

Arrays in Javascript are not associative, because everything in Javascript is an Object. I've found Javascript Garden to be a nice reference of good practices.

Maybe I'm just lazy, but I tend to make a habit out of making my arrays as "shallow" as possible. Like the De La Soul joint, "3's the magic number" for me. ;)

Michael Stilson
  • 340
  • 2
  • 10
  • Thanks for the answer, that was actually the point of my question: **how should I build my objects / arrays?** Does _shallow_ perform better? – Solo Jan 21 '16 at 20:11
  • You are welcome. :) In my humble opinion, yes, shallow is better. In my own experiments, as the Javascript Garden section on arrays suggests, using the classic "for" loop is better for performance than the "for in" construct, i imagine no matter how "deep" you build the arrays. – Michael Stilson Jan 21 '16 at 20:18