Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On Data Structures and Algorithms with JavaScript

You're reading from   Hands-On Data Structures and Algorithms with JavaScript Write efficient code that is highly performant, scalable, and easily testable using JavaScript

Arrow left icon
Product type Paperback
Published in Jan 2018
Publisher Packt
ISBN-13 9781788398558
Length 332 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Kashyap Mukkamala Kashyap Mukkamala
Author Profile Icon Kashyap Mukkamala
Kashyap Mukkamala
Arrow right icon
View More author details
Toc

Examples of time complexity


Let's now examine some examples of time complexity calculations, since in 99% of the cases we need to know the maximum time a function might take to execute; we will be mostly analyzing the worst case time complexity, that is, the upper bound of the rate of growth based on the input of a function.

Constant time

A constant time function is one which takes the same amount of time to execute, irrespective of the size of the input that is passed into the function:

function square(num) {
    return num*num;
}

The preceding code snippet is an example of a constant time function and is denoted by O(1). Constant time algorithms are the most sought out algorithms for obvious reasons, such as them running in a constant time, irrespective of the size of the input. 

Logarithmic time

A Logarithmic time function is one in which the time of execution is proportional to the logarithm of the input size. Consider the following example:

for(var i = 1; i < N; i *= 2) {
    // O(1) operations...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime