<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Muhammad Mustafa</title>
    <description>The latest articles on DEV Community by Muhammad Mustafa (@aisaaswithmustafa).</description>
    <link>https://dev.to/aisaaswithmustafa</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4240643%2F0d5cdc9d-3ef0-4212-8b38-0af964ee4785.jpg</url>
      <title>DEV Community: Muhammad Mustafa</title>
      <link>https://dev.to/aisaaswithmustafa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aisaaswithmustafa"/>
    <language>en</language>
    <item>
      <title>Big O Calculator for JavaScript: Time and Space Complexity</title>
      <dc:creator>Muhammad Mustafa</dc:creator>
      <pubDate>Thu, 19 Nov 2026 21:30:36 +0000</pubDate>
      <link>https://dev.to/aisaaswithmustafa/big-o-calculator-for-javascript-time-and-space-complexity-35fb</link>
      <guid>https://dev.to/aisaaswithmustafa/big-o-calculator-for-javascript-time-and-space-complexity-35fb</guid>
      <description>&lt;p&gt;A Big O calculator for JavaScript is only useful if it shows its work. I built &lt;a href="https://aisaaswithmustafa.com/tools/complexity-coach/" rel="noopener noreferrer"&gt;Complexity Coach&lt;/a&gt;, a free tool that reads your code and estimates its time and space complexity, because most answers I found online were either a bare "O(n)" with no reason or a confident guess.&lt;/p&gt;

&lt;p&gt;In this guide I explain how Big O notation maps to real JavaScript, which built-in methods hide extra cost, how time and space complexity differ, and where a static calculator, including mine, should refuse to guess.&lt;/p&gt;

&lt;h2&gt;
  
  
  Big O notation in plain words
&lt;/h2&gt;

&lt;p&gt;Big O describes how the work grows when the input grows. It ignores the exact number of milliseconds, because that depends on the machine. It only asks: if the input doubles, what happens to the work?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;O(1):&lt;/strong&gt; the work stays the same, like reading &lt;code&gt;arr[5]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;O(log n):&lt;/strong&gt; the work grows very slowly, like binary search, which halves the range each step.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;O(n):&lt;/strong&gt; the work grows with the input, like one loop over an array.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;O(n log n):&lt;/strong&gt; typical for good sorting.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;O(n²):&lt;/strong&gt; every item compared with every other item, usually two nested loops.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two rules do most of the work. Drop constants, so O(2n) is just O(n). Keep the biggest term, so O(n² + n) becomes O(n²). The second rule has a catch I will come back to, because it only applies when the terms use the same input.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to read Big O from JavaScript code
&lt;/h2&gt;

&lt;p&gt;You do not need maths to estimate Big O for most interview or product code. You need to count how many times each line can run.&lt;/p&gt;

&lt;h3&gt;
  
  
  Loops multiply, separate blocks add
&lt;/h3&gt;

&lt;p&gt;One loop over &lt;code&gt;n&lt;/code&gt; items is O(n). A loop inside another loop multiplies: O(n × n), or O(n²). Two loops one after the other add: O(n + n), which is still O(n).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;hasDuplicate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;        &lt;span class="c1"&gt;// n times&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;// up to n times each&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// Time: O(n²)  Extra space: O(1)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Different inputs get different letters
&lt;/h3&gt;

&lt;p&gt;This is the most common mistake I see. If the outer loop runs over &lt;code&gt;comments&lt;/code&gt; and the inner loop runs over &lt;code&gt;bannedWords&lt;/code&gt;, the answer is O(m × n), not O(n²). Writing n² hides the fact that a long banned-word list hurts just as much as a long comment list.&lt;/p&gt;

&lt;p&gt;The same applies to adding terms. O(m × L + n) cannot be simplified to O(m × L), because n is a different input. "Keep the biggest term" only works when the bigger term contains all the same sizes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Halving means logarithmic
&lt;/h3&gt;

&lt;p&gt;When each step cuts the remaining range in half, like binary search with a &lt;code&gt;mid&lt;/code&gt; index, the loop runs about log₂ n times. For a million items, that is around 20 steps.&lt;/p&gt;

&lt;h3&gt;
  
  
  Recursion needs a recurrence, not a glance
&lt;/h3&gt;

&lt;p&gt;Recursive code is where quick estimates go wrong. Count how many calls each call makes and how deep the calls go. The naive Fibonacci function below calls itself twice per step, so the number of calls roughly doubles at each level:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// two calls per call: about O(2^n)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fibMemo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;memo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fibMemo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;fibMemo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// each n is computed once: O(n) time, O(n) space&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The memoised version does the same maths but stores each answer, so every value of n is computed once. The call stack still grows to depth n, which is why its space is O(n), not O(1).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fob74e0487xom35lvuwjp.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fob74e0487xom35lvuwjp.jpg" alt="Notebook sketch of nested loop blocks drawn to estimate Big O notation" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Counting how often each line runs is usually enough: nested loops multiply, separate blocks add.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Hidden costs in JavaScript array and string methods
&lt;/h2&gt;

&lt;p&gt;Many JavaScript methods look like one cheap line but contain a loop. A single &lt;code&gt;includes&lt;/code&gt; inside a loop is often what turns O(n) code into O(n²).&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;th&gt;Extra space&lt;/th&gt;
&lt;th&gt;Note&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;arr[i]&lt;/code&gt;, &lt;code&gt;push&lt;/code&gt;, &lt;code&gt;pop&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;push&lt;/code&gt; is O(1) on average&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;includes&lt;/code&gt;, &lt;code&gt;indexOf&lt;/code&gt;, &lt;code&gt;find&lt;/code&gt;, &lt;code&gt;some&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;Linear scan of the array&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;Build a new array&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;slice&lt;/code&gt;, &lt;code&gt;concat&lt;/code&gt;, &lt;code&gt;[...arr]&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;O(k)&lt;/td&gt;
&lt;td&gt;O(k)&lt;/td&gt;
&lt;td&gt;Copy k items&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;shift&lt;/code&gt;, &lt;code&gt;unshift&lt;/code&gt;, &lt;code&gt;splice&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Usually O(n)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;Other items move position&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sort&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;O(n log n)&lt;/td&gt;
&lt;td&gt;Up to O(n)&lt;/td&gt;
&lt;td&gt;V8 uses TimSort&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;Map&lt;/code&gt; / &lt;code&gt;Set&lt;/code&gt; get, has, set&lt;/td&gt;
&lt;td&gt;O(1) on average&lt;/td&gt;
&lt;td&gt;One entry each&lt;/td&gt;
&lt;td&gt;Hash tables in practice&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;split&lt;/code&gt;, &lt;code&gt;trim&lt;/code&gt;, &lt;code&gt;toLowerCase&lt;/code&gt;, &lt;code&gt;replace&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;O(L)&lt;/td&gt;
&lt;td&gt;O(L)&lt;/td&gt;
&lt;td&gt;L is the string length&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two of these deserve a source. V8, the engine in Chrome and Node.js, has used TimSort for &lt;code&gt;Array.prototype.sort&lt;/code&gt; since &lt;a href="https://v8.dev/blog/array-sort" rel="noopener noreferrer"&gt;version 7.0&lt;/a&gt;, which gives O(n log n) in the worst case. For &lt;code&gt;Map&lt;/code&gt; and &lt;code&gt;Set&lt;/code&gt;, the &lt;a href="https://tc39.es/ecma262/multipage/keyed-collections.html" rel="noopener noreferrer"&gt;ECMAScript specification&lt;/a&gt; only requires access times that are sublinear on average. Engines use hash tables, so O(1) on average is the honest way to say it.&lt;/p&gt;

&lt;p&gt;The fix for a hidden scan is usually a Set:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Slow: includes scans bannedWords for every word, O(w × b)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;isClean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;bannedWords&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Fast: build the Set once in O(b), then each lookup is O(1) on average&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;banned&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bannedWords&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;isCleanFast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;banned&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two more patterns catch people in code reviews. The first is a queue built on &lt;code&gt;shift()&lt;/code&gt;. In a breadth-first search, calling &lt;code&gt;queue.shift()&lt;/code&gt; on every step usually moves all remaining items forward, so a loop that looks linear can become O(n²) on large inputs. Keeping a head index and reading &lt;code&gt;queue[head++]&lt;/code&gt; avoids the move.&lt;/p&gt;

&lt;p&gt;The second is copying inside a loop. Writing &lt;code&gt;acc = [...acc, item]&lt;/code&gt; inside &lt;code&gt;reduce&lt;/code&gt; copies the whole array on every step, which is also O(n²) time and creates a lot of short-lived garbage. Pushing into one array keeps it at O(n). Neither pattern is wrong for small lists, but both are worth a comment when the list comes from users.&lt;/p&gt;

&lt;p&gt;Strings need their own letter. &lt;code&gt;comment.split(" ")&lt;/code&gt; reads the whole comment, so its cost depends on the comment's length, not on how many comments there are. I use L for that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Time and space complexity, including the output
&lt;/h2&gt;

&lt;p&gt;Time complexity counts steps. Space complexity counts extra memory. Interview answers usually separate two kinds of space:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Auxiliary space:&lt;/strong&gt; memory your algorithm needs while working, such as a Set of seen values or a temporary copy.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Space including output:&lt;/strong&gt; auxiliary space plus the array you return.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A function that fills and returns a result array of n items has O(n) total space, but its auxiliary space can still be O(1):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;doubled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// the output: counts only in total space&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// one index variable: O(1) extra&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// Time: O(n)  Auxiliary space: O(1)  Space including output: O(n)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Mixing these two up is why two people can look at the same code and give different answers that are both partly right. Complexity Coach shows both values side by side for this reason.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I built Complexity Coach, and where it refuses to guess
&lt;/h2&gt;

&lt;p&gt;Complexity Coach runs in your browser. It parses the code into an abstract syntax tree (AST) with the Acorn parser, so nothing is sent to a server. It then walks the tree, tracks how loops nest, recognises known array, Map, Set and string operations, highlights the lines that drive the result, and gives a confidence level: High, Medium or Manual review needed.&lt;/p&gt;

&lt;p&gt;The rule I care about most is that it does not invent answers. Recursion, calls to helper functions it cannot see, loops without a clear bound and dynamic property access all return "Manual review needed" with the reason and the exact line. A wrong O(1) is worse than no answer, because people trust it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9ilfjbo1oze1rf89y1d0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9ilfjbo1oze1rf89y1d0.jpg" alt="Two developers reviewing JavaScript code for time and space complexity" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A complexity estimate is most useful in code review, when it points to the exact line that changes the result.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That rule also showed me a gap. When I tested a comment filter that split each comment into words and checked them against a Set of banned words, the tool answered "Manual review needed", because it did not know &lt;code&gt;split()&lt;/code&gt;. Honest, but not useful for very common code. I added the common string methods with L as the string length, and the same code now returns O(m × L + n) time, O(n + L) auxiliary space and O(m + n + L) including the output.&lt;/p&gt;

&lt;p&gt;The same test exposed a second problem: an earlier version dropped the n term and answered O(m × L). Building a Set of n banned words is real work that the bigger term does not cover, so smaller terms are now kept whenever they use a different input.&lt;/p&gt;

&lt;h3&gt;
  
  
  Static analysis or a runtime benchmark?
&lt;/h3&gt;

&lt;p&gt;There are two kinds of Big O calculator. Runtime tools, such as the &lt;a href="https://www.npmjs.com/package/big-o-calculator" rel="noopener noreferrer"&gt;big-o-calculator&lt;/a&gt; package on npm, run your function with growing input sizes and fit the timings to a curve. Static tools, like Complexity Coach, read the code without running it.&lt;/p&gt;

&lt;p&gt;Runtime measurement sees real data, but timings are noisy because of JIT warm-up and garbage collection, and the result depends on the test inputs you chose. Static analysis explains why the code grows the way it does, but it cannot know the shape of your production data. I use the static estimate while writing and reviewing code, and a benchmark when a specific function is actually slow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical takeaway
&lt;/h2&gt;

&lt;p&gt;Before you trust any Big O calculator, check five things yourself:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Give each input its own letter, so m × n never hides as n².&lt;/li&gt;
&lt;li&gt; Multiply nested loops and add separate blocks.&lt;/li&gt;
&lt;li&gt; Look inside one-line methods such as &lt;code&gt;includes&lt;/code&gt;, &lt;code&gt;slice&lt;/code&gt;, &lt;code&gt;sort&lt;/code&gt; and &lt;code&gt;split&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; Report auxiliary space and space including the output separately.&lt;/li&gt;
&lt;li&gt; Treat recursion as a manual task: write the recurrence instead of guessing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you want a second opinion with the evidence on each line, paste your function into &lt;a href="https://aisaaswithmustafa.com/tools/complexity-coach/" rel="noopener noreferrer"&gt;Complexity Coach&lt;/a&gt;; it is one of my &lt;a href="https://aisaaswithmustafa.com/tools/index.html" rel="noopener noreferrer"&gt;free developer tools&lt;/a&gt;. If you need help with performance in a real Node.js or SaaS codebase, &lt;a href="https://aisaaswithmustafa.com/#contact" rel="noopener noreferrer"&gt;get in touch&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I first published this on my blog: &lt;a href="https://aisaaswithmustafa.com/blog/big-o-calculator-javascript.html" rel="noopener noreferrer"&gt;Big O Calculator for JavaScript: Time and Space Complexity&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>javascript</category>
      <category>performance</category>
      <category>tools</category>
    </item>
  </channel>
</rss>
