// Slower
function addUptoNSlower(n){
let result = 0;
for(let i = 1;i<=n;i++){
result += i;
}
return result;
}
let t1 = perfrmance.now()
console.log(addUptoNSlower(5))
// Faster
function addUptoNFaster(n){
return n * (n+1) /2
}
console.log(addUptoNFaster(3))
// When to use objects
// When you don't need order
// When you need fast access / insertion and removal
// OBJECTS
// Unordered, key value pairs!
// Big O of Objects
// Insertion - O(1)
// Removal - O(1)
// Searching - O(N)
// Access - O(1)
// Big O of Object Methods
// Object.keys - O(N)
// Object.values - O(N)
// Object.entries - O(N)
// hasOwnProperty - O(1)
let instructor = {
firstName: "Kelly",
isInstructor: true,
favoriteNumbers: [1,2,3,4]
}
// ARRAYS
// Ordered lists!
let names = ["Michael", "Melissa", "Andrea"];
let values = [true, {}, [], 2, "awesome"];
// WHEN TO USE ARRAYS
// When you need order
// When you need fast access / insertion and removal (sort of....)
// Big O of Arrays
// Insertion - It depends....
// Removal - It depends....
// Searching - O(N)
// Access - O(1)
// Big O of Array Operations
// push - O(1)
// pop - O(1)
// shift - O(N)
// unshift - O(N)
// concat - O(N)
// slice - O(N)
// splice - O(N)
// sort - O(N * log N)
// forEach/map/filter/reduce/etc. - O(N)
// Limitations of Arrays
// Inserting at the beginning is not as easy as we might think! There are more efficient data structures for that!