Space complexity is defined as how much additional space the algorithm needs in terms of the N elements. And even though according to the docs, the sort method sorts a list in place, it does use some additional space, as stated in the description of the implementation:

timsort can require a temp array containing as many as N//2 pointers, which means as many as 2*N extra bytes on 32-bit boxes. It can be expected to require a temp array this large when sorting random data; on data with significant structure, it may get away without using any extra heap memory.

Therefore the worst case space complexity is O(N) and best case O(1)

Answer from damores on Stack Overflow

Space complexity is defined as how much additional space the algorithm needs in terms of the N elements. And even though according to the docs, the sort method sorts a list in place, it does use some additional space, as stated in the description of the implementation:

timsort can require a temp array containing as many as N//2 pointers, which means as many as 2*N extra bytes on 32-bit boxes. It can be expected to require a temp array this large when sorting random data; on data with significant structure, it may get away without using any extra heap memory.

Therefore the worst case space complexity is O(N) and best case O(1)

Answer from damores on Stack Overflow
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Timsort
Timsort - Wikipedia
February 17, 2026 - The original merge sort implementation is not in-place and it has a space overhead of N (data size). In-place merge sort implementations exist, but have a high time overhead. In order to achieve a middle term, Timsort performs a merge sort with a small time overhead and smaller space overhead than N.
๐ŸŒ
Medium
ericmervin.medium.com โ€บ what-is-timsort-76173b49bd16
What is Timsort?
March 24, 2021 - If we talk in terms of space complexity, Timsort has a complexity of O(n), which when compared to other algorithms like Quicksort that have a complexity O(1), suggests that maybe it lies on the more inadequate side of the spectrum.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ timsort
TimSort - GeeksforGeeks
January 30, 2026 - Auxiliary Space: O(n), additional space is required for merging runs. Stability: Yes, Tim Sort is a stable sorting algorithm (maintains the relative order of equal elements). Timsortโ€™s time complexity comes from the combination of two phases โ€” sorting small runs using Insertion Sort and merging those runs using a Merge Sort like process.
๐ŸŒ
AlgoWalker
algowalker.com โ€บ tim-sort.html
Efficient Tim Sort Algorithm | Sorting Made Easy - AlgoWalker
The time complexity of Timsort is O(n log n) in the worst case and average case, and O(n) in the best case (when the input array is already sorted or nearly sorted). This makes Timsort one of the fastest sorting algorithms for many real-world ...
๐ŸŒ
C2
wiki.c2.com
Tim Sort Simplified
This site uses features not available in older browsers
Find elsewhere
๐ŸŒ
GitHub
gist.github.com โ€บ NightStrang6r โ€บ cd88b10faaf1583736cbe0675e09079a
Timsort.md ยท GitHub
However, its best-case (O(n)) and average-case complexities can be significantly better due to the following factors: Natural Runs: by exploiting naturally occurring runs, Timsort can reduce the number of merge operations, making it faster on ...
๐ŸŒ
Code of Code
codeofcode.org โ€บ home โ€บ timsort in c++
Timsort in C++ - Code of Code
January 24, 2023 - The average case time complexity of Timsort is also O(n log n). This is because the algorithm is designed to be adaptive and will use the best sorting technique for the data being sorted.
๐ŸŒ
Number Analytics
numberanalytics.com โ€บ blog โ€บ timsort-computational-complexity-analysis
Computational Complexity of Timsort
Timsort is designed to handle large datasets efficiently due to its $O(n \log n)$ time complexity and $O(n)$ space complexity.
๐ŸŒ
Number Analytics
numberanalytics.com โ€บ blog โ€บ timsort-in-depth-analysis-and-comparison
Timsort in Depth: Analysis and Comparison
This is because Timsort is based on merge sort, which has a time complexity of $O(n \log n)$. However, Timsort's best-case time complexity is $O(n)$, which occurs when the input is already sorted or nearly sorted. This is because Timsort takes advantage of the existing order in the input data.
๐ŸŒ
Hacker News
news.ycombinator.com โ€บ item
Note that Timsort uses O(n) extra space: sometimes this can be undesirable. | Hacker News
October 9, 2019 - Stable sorts often do require that, (mergesort is usually stabble, heapsort and quicksort are inherently not), but even that's not required - there is a completely in-place variant of merge sort that only requires O(log n) space for stack (like quicksort; heapsort is O(1)). See e.g. ...
๐ŸŒ
OpenGenus
iq.opengenus.org โ€บ tim-sort
Tim Sort
June 7, 2020 - Let's give an example: run1: [1, 3, 5, 7, 9 ... 2n+1] run2: [0, 2, 4, 6, 8 ... 2n] In this way, run1[0] is only larger than run2[0]. Each time we do Binary Search, we can only get a number of results, plus n times of Binary Search, so the time complexity changes from O(N) to O(NlogN).
๐ŸŒ
Medium
muskanvaswan.medium.com โ€บ tim-sort-48bffd550a9b
Tim Sort. Introduction and Background | by Muskan Vaswan | Medium
April 5, 2022 - The algorithm requires O(nlogn) comparisons to sort an array of n elements. Hence, the time complexity is of the order of [Big Theta]: O(nlogn).
Top answer
1 of 3
122

Firefox uses merge sort. Chrome, as of version 70, uses a hybrid of merge sort and insertion sort called Timsort.

The time complexity of merge sort is O(n log n). While the specification does not specify the sorting algorithm to use, in any serious environment, you can probably expect that sorting larger arrays does not take longer than O(n log n) (because if it did, it would be easy to change to a much faster algorithm like merge sort, or some other log-linear method).

While comparison sorts like merge sort have a lower bound of O(n log n) (i.e. they take at least this long to complete), Timsort takes advantages of "runs" of already ordered data and so has a lower bound of O(n).

2 of 3
48

Theory and practice: In theory there is no difference between theory and practice, but in practice there is.

  • Theory: everything is clear, but nothing works;
  • Practice: everything works, but nothing is clear;
  • Sometimes theory meets practice: nothing works and nothing is clear.

Big O notation is great for assessing the scalability of an algorithm, but does not provide a means of direct comparison of performance between implementations of an algorithm...

Case in point is the implementation of Array.sort() within browsers. Despite Timsort having a better Big O profile than Merge sort (see https://www.bigocheatsheet.com/), empirical testing shows that the implementation of Timsort in Chrome's V8 engine is clearly outperformed on average by the implementation of Merge sort in Firefox.

The charts below each show two sets of data points:

  • The blue data is the performance of Array.sort() for 500 test cases of random length arrays (from 100 to 500,000 elements) randomly filled with integers. The curved line shows the median of the data in the form of N * Log( N ), and the dotted curves show the 95% bounds, again in the form of N * Log( N ). That is to say, 95% of the data points lie between these curves.
  • The orange data shows the performance of Array.sort() for a mostly sorted array. Specifically, ~2% of the values in the array are re-randomized and then Array.sort() is applied again. In this case, the solid and dotted lines are linear measures of performance, not logarithmatic.

Furthermore, Big O notation provides a general rule of thumb to expect from the scalability of the algorithm, but does not address the variability. The Chrome V8 implementation of the Timsort algorithm has a wider variability in its execution than the Firefox Merge sort, and despite Timsort's better Big O profile, even Timsort's best times are not better than the Merge sort's worst times. At the risk of starting a religious war, this does not mean that the Timsort is worse than Merge sort, as this could simply be a case of better overall performance by Firefox's implementation of JavaScript.

The data for the charts above was generated from the following code on my Acer Aspire E5-5775G Signature Edition having an Intel Core i5-7200U CPU @2.50GHz and 8GB of RAM. The data was then imported into Excel, analyzed for the 95% bounding range, and then charted. The axes scales on the charts are normalized for ease of visual comparison.

  function generateDataPoints( qtyOfTests, arrayRange, valueRange, nearlySortedChange ) {
  
    let loadingTheArray = [];
    let randomSortMetrics = [];
    let nearlySortedMetrics = [];
    
    for ( let testNo = 0; testNo < qtyOfTests; testNo++ ) {
      if ( testNo % 10 === 0 ) console.log( testNo );
      
      // Random determine the size of the array given the range, and then
      // randomly fill the array with values.
      let testArray = [];      
      let testArrayLen = Math.round( Math.random() * ( arrayRange.hi - arrayRange.lo ) ) + arrayRange.lo;
      
      start = performance.now();
      for ( let v = 0; v < testArrayLen; v++ ) {
        testArray[ v ] = Math.round( Math.random() * ( valueRange.hi - valueRange.lo ) ) + valueRange.lo;
      }
      end = performance.now();
      
      loadingTheArray[ testNo ] = { x: testArrayLen, y: Math.floor( end - start ) };
      
      // Perform the sort and capture the result.
      start = performance.now();
        testArray.sort( (a, b ) => a - b );
      end = performance.now();
      
      randomSortMetrics[ testNo ] = { x: testArrayLen, y: Math.floor( end - start ) };
      
      // Now, let's change a portion of the sorted values and sort again.
      let qtyOfValuesToChange = testArrayLen * nearlySortedChange;
      for ( let i = 0; i < qtyOfValuesToChange; i++ ) {
        let v = Math.round( Math.random() * testArrayLen );
        testArray[ v ] = Math.round( Math.random() * ( valueRange.hi - valueRange.lo ) ) + valueRange.lo;
      }
      
      start = performance.now();
        testArray.sort( (a, b ) => a - b );
      end = performance.now();
      
      nearlySortedMetrics[ testNo ] = { x: testArrayLen, y: Math.floor( end - start ) };

    }
    
    return  [ loadingTheArray, randomSortMetrics, nearlySortedMetrics ];
    
  }
  
  // Let's start running tests!
  let arraySizeRange = { lo: 100, hi: 500000 };
  let valueRange = { lo: 0, hi: 2 ** 32 - 1 };
  let results = generateDataPoints( 500, arraySizeRange, valueRange, 0.02 );
  
  
  let tabFormat = 'No Of Elements\tTime to Load Array\tFull Sort\tMostly Sorted\n';
  for ( let i = 0; i < results[0].length; i++ ) {
    tabFormat += `${results[0][i].x}\t${results[0][i].y}\t${results[1][i].y}\t${results[2][i].y}\n`;
  }
  console.log( tabFormat );

The takeaway is that the performance of an algorithm, ostensibly being better based on Big O notation, has many factors that drive its overall performance, and a better Big O does not necessarily translate to better performance...

๐ŸŒ
Dagstuhl
drops.dagstuhl.de โ€บ storage โ€บ 00lipics โ€บ lipics-vol112-esa2018 โ€บ LIPIcs.ESA.2018.4 โ€บ LIPIcs.ESA.2018.4.pdf pdf
On the Worst-Case Complexity of TimSort Nicolas Auger
To obtain the O(n log ฯ) complexity, we need to distinguish several situations. First, consider the sequence of Cases #1 to #5 triggered during the execution of the main loop ยท of TimSort. It can be seen as a word on the alphabet {#1, .
๐ŸŒ
fd93
fd93.me โ€บ pages โ€บ cs-101-08
fd93 โ€“ Timsort and Other Compound Sorting Algorithms
Therefore we usually consider timsort's worst case time complexity to be O(nlogn). By looking at the worst case and the best case, we can see that for most lists we will do some number of comparisons more than n but less than the worst case.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ algorithms โ€บ sorting โ€บ quicksort vs. timsort
Quicksort vs. Timsort | Baeldung on Computer Science
June 28, 2024 - As we saw previously, Timsort is a great algorithm with many benefits and a good performance overall. The first thing that can be considered a negative point is space complexity. Another issue with Timsort is that itโ€™s much more complex than Quicksort. Even a well-tuned implementation of a Quicksort can be written down in twenty or so lines of code.
๐ŸŒ
Python Pool
pythonpool.com โ€บ home โ€บ blog โ€บ timsort: algorithm and implementation in python
TimSort: Algorithm and Implementation in Python - Python Pool
August 6, 2021 - Worst-case space complexity: O(n) It is a stable sorting algorithm ยท Works for real-time data ยท Insertion Sort in Python [Program, Algorithm, Example] Shell Sort Algorithm and Program in Python ยท Learning Various Ways in Python to Sort the ...