Advertisement
samimwebdev

Array object complexity

Jun 17th, 2022
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const arr = ['a', 'b', 'c']
  2. // console.log(arr.length)
  3.  
  4. //access by index O(1)
  5. // console.log(arr[arr.length - 1])
  6.  
  7. //adding item/element at the end of the array O(1)
  8. // arr[arr.length] = 'd'
  9. // arr.push('d')
  10.  
  11. // //removing element from end of the array time complexity O(1)
  12. // arr.pop()
  13.  
  14. //at the beginning of array adding element O(n)
  15. // arr.unshift('z')
  16. //at the beginning of array removing element O(n)
  17. // arr.shift('z')
  18.  
  19. //finding element from an array O(n)
  20. // for(let elm of arr){
  21. //     if(elm === 'c') console.log(true)
  22. // }
  23.  
  24. //forEach, map, filter, reduce, slice, splice O(n)
  25. // console.log(arr)
  26.  
  27. //Object
  28.  
  29. const obj = {
  30.   name: 'samim',
  31.   email: 'samimfazlu@gmail.com',
  32. }
  33.  
  34. //accessing element O(1)
  35. console.log(obj.name)
  36. //adding/inserting element O(1)
  37. obj.profession = 'programmer'
  38.  
  39. //delete/removal element O(1)
  40. delete obj.profession
  41. console.log(obj)
  42.  
  43. //searching O(1)
  44. console.log('email' in obj)
  45.  
  46. //searching O(n)
  47. // for(let key in obj){
  48. //     console.log(obj[key])
  49. //     console.log(key)
  50. // }
  51.  
  52. //Object.keys() O(n)
  53. // console.log(Object.keys(obj))
  54. // //Objet.values() O(n)
  55. // console.log(Object.values(obj))
  56.  
  57. // //Object.entries() O(n)
  58. // console.log(Object.entries(obj))
  59.  
  60. // console.log(obj.name)
  61. // console.log(obj["name"])
  62. // console.log(obj['1email']) //invalid key must be accessed by []
  63.  
  64. //when to use array
  65. //when order is important
  66. // when you need faster access (by index) or
  67. //adding or removal(at the end)
  68.  
  69. //when to use object
  70. //when order isn't important
  71. // when you need faster access or removal
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement