DEV Community

EmanSaeed331
EmanSaeed331

Posted on

Big O

Big O used to classify algorithms according to how their run time or space.

Big O shorthand

1. Arithmetic operators are constant.
2. Variable assignment is constant.
3. Accessing elements in an array (by index ) or an object (by key ) is constant.
4. in loop the complexity is the length of the loop times the complexity of whatever happens inside this loop.

Space complexity rules

1. Most Primitives (Boolean, numbers , undefined , null ) are constant space .
2. string require O(N) space where n is length of the string.
3. Reference type are generally O(N) space where n is the length (for arrays or the number of keys for objects )

Big O for Objects

1. insertion O(1)
2. Removing O(1)
3. Searching O(N)
4. Accessing O(1)

Big O for Objects Methods

1. Object. Keys O(N)
2. Object. Values O(N)
3. Object. entries O(N)
4. has Own Property O(1)

Big O for arrays

1. insertion O(1) if we insert at the end but at beginning of array O(N)
2. Removing O(1) if we remove at the end but at beginning of array O(N)
3. Searching O(N)
4. Accessing O(1)

Big O for arrays Methods

1. push O(1)
2. pop O(1)
3. shift O(N)
4. unshift O(N)
5. concatenate O(N)
6. slice O(N)
7. splice O(N)
8. sort O(N Log N)
9. forEach / map / filter / reduce O(N)

Top comments (0)