Thinking like a computer scientist: Considering the time complexity in your JavaScript code algorithm
Big O notation is an essential concept in computer science, especially when dealing with large amounts of data. It provides a way to analyze and measure the performance of an algorithm in terms of the input size. In JavaScript, when working with data, it is essential to consider Big O notation to ensure that your code is optimized for efficiency. In this blog, we will discuss how to consider Big O notation when handling data using JavaScript with code examples.
What is Big O Notation?
Big O notation is a mathematical notation that describes the upper bound of the time complexity or space complexity of an algorithm. It provides an estimate of how the algorithm's running time or memory usage grows as the input size increases. The time complexity of an algorithm is measured by the number of operations the algorithm performs as a function of the input size.
In Big O notation, we use the letter "O" followed by a function that represents the upper bound of the algorithm's time complexity. For example, an algorithm with a time complexity of O(n) means that it's running time increases linearly with the input size.
Examples of Big O Notation in JavaScript:
Let's take a look at some examples of Big O notation in JavaScript.
Example 1: Linear Search
Linear search is a basic search algorithm that checks each item in an array until it finds the desired item. The worst-case time complexity of linear search is O(n), where n is the size of the array.
The time spent to finish processing the data is relative to the size of it.
function linearSearch(arr, item) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === item) {
return i;
}
}
return -1;
}
const arr = [1, 2, 3, 4, 5];
const index = linearSearch(arr, 4);
console.log(index); // Output: 3
Example 2: Binary Search
Binary search is a more efficient search algorithm that works by dividing the search interval in half repeatedly. The worst-case time complexity of binary search is O(log n), where n is the size of the array.
function binarySearch(arr, item) {
let low = 0;
let high = arr.length - 1;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
if (arr[mid] === item) {
return mid;
} else if (arr[mid] < item) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
const arr = [1, 2, 3, 4, 5];
const index = binarySearch(arr, 4);
console.log(index); // Output: 3
Example 3: Bubble Sort
Bubble sort is a simple sorting algorithm that repeatedly swaps adjacent elements if they are in the wrong order. The worst-case time complexity of bubble sort is O(n^2), where n is the size of the array.
function bubbleSort(arr) {
const n = arr.length;
for (let i = 0; i < n - 1; i++) {
for (let j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
const temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
const arr = [5, 3, 1, 4, 2];
const sortedArr = bubbleSort(arr);
console.log(sortedArr); //
Output: [1, 2, 3, 4, 5]
Big O notation is an essential concept to consider when handling data using JavaScript. It provides a way to measure the performance of an algorithm in terms of the input size. By analyzing the time complexity of an algorithm, we can determine the efficiency of the algorithm and optimize it for better performance. In the examples above, we saw how different algorithms have different time complexities and how we can use Big O notation to describe their performance. When working with large datasets or complex algorithms, it is crucial to consider Big O notation to ensure that our code is optimized for efficiency.
About the author
Joff Tiquez, hailing from Manila, Philippines, is the individual behind the establishment of OSSPH. He is a web developer who strongly supports open source and has been overseeing projects like Vue Stripe for an extended period. To get in touch with Joff, you can visit https://bento.me/jofftiquez.