JavaScript 개체, 배열 및 해당 메서드의 Big O 시간 복잡성
개체란 무엇입니까?
An object is a data container, where the collection of properties save in key-value pair format between the curly braces {}.
Generally, the key-value pair is known as the property name and property value, where the property name should be unique, and it stays on the left side before colon ":" and property values on the right side after the colon.
To separate individual property, we use a comma.
Example:
var obj = {name:"Himanshu", jobProfile:"SDE"}
Note: If the same property name with a different value is created again in the object, JavaScript considers only new property.
객체를 사용할 때
We should use it when we don't need to save data in order, and at the same time required to get fast access, fast insertion, and removal.
객체와 그 메서드가 얼마나 비용이 많이 드는지 봅시다.
- Insertion Operation: Happens in O(1)
- Removal Operation: Happens in O(1)
- Searching Operation: Happens in O(N)
- Access Operation: Happens in O(1)
- Object.keys Method: Happens in O(N)
- Object.values Method: Happens in O(N)
- Object.entries Method: Happens in O(N)
- hasOwnProperty Method: Happens in O(N)
배열이란 무엇입니까?
var arr = [1,2,3,4,null,() => 5]
An array is a data container that follows the linear data structure, which means all elements store in a sequential manner. The great thing about an Array in javascript is:
- It is dynamic, which means we can store almost all types of value in an array.
- As it is a dynamic array, we don't need to specify the length of an Array in advance like other languages.
배열을 사용하는 경우
We should use it when we need to save data in order, and at the same time required to get fast access, fast insertion, and removal.
Note:Fast Insertion and removal depend on where you are adding and removing from
얼마나 비용이 많이 드는 Array와 그 방법을 봅시다.
- Insertion at the end of an Array: O(1)
- Removal at the end of an Array: O(1)
- Insertion at the beginning & mid of a non-empty Array: O(1)
- Removal of item from the start & mid of a non-empty Array: O(1)
- Searching if an Array unsorted: O(N)
- Searching if an Array sorted: Depends on the algorithm
- Access: O(1)
- push (inserting element at the end of an array): O(1)
- pop (deleting an element from the end of an array): O(1)
- Shift(opposite to push, and it shifts next elements to next index position): O(N)
- unshift (opposite to pop, and it shifts elements to the previous index position): O(N)
- concat: O(N)
- slice: O(N)
- splice: O(N)
- sort: depends
- forEach, map, filter, reduce: O(N)
Reference
이 문제에 관하여(JavaScript 개체, 배열 및 해당 메서드의 Big O 시간 복잡성), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/himanshukanojiya/big-o-time-complexities-of-javascript-object-arrays-and-their-methods-5gpk텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)