Why is TIME and SPACE complexity important for you?

Ms Chi
2 min readJul 20, 2022

Purpose

Time complexity measures the total time taken to execute each of the statement of an algorithm.

Space complexity measures the total amount of memory space used to run a algorithm including the inputs.

Why are they essential?

There are millions ways of coding to achieve the same target. However the time and space taken may be varied based on the algorithm. And these two factors are the essential keys to determine the performance of web/mobile product. Do you want to use a product which gives SLOW respond?

Scenario

We are given one task today by finding the median of two arrays.
At the first glance, we may plan to do merging and sorting both array into one, then only start to look for median index.

However in this way, we will create a new array. There are several ways to merge the array such as by spreading […array1, array2].sort((a, b) => a-b) OR by concat array1.concat(array2).sort((a, b) => a-b)

According to the MDN, the time complexity and space complexity of built-in Array.prototype.sort() cannot be guaranteed as it depends on the implementation.

Explanation of Javascript build-in sort() from MDN

--

--

Ms Chi
Ms Chi

Written by Ms Chi

A typescript JS people who breathes React and Node. Long term learning curve is a must to keep us fresh and competitive. Don’t be overwhelmed by the speed.

No responses yet