Time Complexities

The table below provides an overview of time complexities for various data structures, algorithms, and library functions in popular languages.

Code Time Complexity Space Complexity Language Comment
strlen() O(n){O(n)} ~ C/C++ Found in string.h, returns the length of a string.
push() O(1){O(1)} ~ JavaScript Array method, adds a new element to the end of an array.
pop() O(1){O(1)} ~ JavaScript Array method, deletes the last element in an array.
shift() O(n){O(n)} ~ JavaScript Array method, deletes the first element in an array.
unshift() O(n){O(n)} ~ JavaScript

Array method, adds one or more elements at the beginning of an array.

splice() O(n){O(n)} ~ JavaScript

Removes, adds, or replaces a new element by index (note: not a very well designed library function; code bases using splice tend to be buggy).

sort() O(nlogn){O(n \log n)} ~ JavaScript Modifies the array by calling a compare function. If the compare function is not provided, the default order is by Unicode value.
concat() O(n){O(n)} ~ JavaScript Creates a new array by merging two or more arrays.
slice() O(n){O(n)} ~ JavaScript Returns a copy of a sub-array between two indices, the start and the end.
indexOf() O(n){O(n)} ~ JavaScript Array method, returns the first index of the argument if found, otherwise returns -1.
forEach() O(n){O(n)} ~ JavaScript Array method, executes a function for each array element.
for...of O(n){O(n)} ~ JavaScript Object method, iterates over an object's keys.
map() O(n){O(n)} ~ JavaScript Array method, creates a new array composed of the result of calling callback function argument on each array element.
filter() O(n){O(n)} ~ JavaScript Array method, creates a new array composed of elements that return the call back function true.
reduce() O(n){O(n)} ~ JavaScript Object array method, returns a single value from applying the reduction function for each element.
... (JavaScript spread syntax) O(n){O(n)} ~ JavaScript Expands an array or object into its individual elements or key-value pairs. Frequently used within the reduce() method, leading to a time complexity of O(n2).{O(n^2).}