The Basics of Time Complexity

The wonderful thing about solving an algorithm is there are generally various solutions. Many ask what is the best solution? Opinions on the best may be impacted by personal preferences, but the efficiency of your code is most important. Big O notation is used in computer science and mathematics to describe how algorithms respond to different inputs. Time Complexity is the relation of input size and the algorithm’s execution time. In Javascript, the input usually refers to an array or object. This blog post will cover four of the most basic types of time complexities of Big O notation: Constant, Logarithmic, Linear, and Quadratic.

Even though not always possible, Constant time complexity is the goal to strive for when implementing code. Then, Logarithmic and Linear. Quadratic is one of the worse for time complexity, but sometimes unavoidable.

Imgur

Example: Imgur

Imgur

Example: Imgur

Imgur

Example: Imgur

Imgur

Example: Imgur

By understanding time complexity at basic level gives you tools to write better code. For more information on common algorithms’ time complexity: http://bigocheatsheet.com/

Written on January 26, 2016