Socket
Socket
Sign inDemoInstall

timsort

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    timsort

TimSort: Fast Sorting for Node.js


Version published
Weekly downloads
2.7M
decreased by-18.27%
Maintainers
1
Install size
58.0 kB
Created
Weekly downloads
 

Package description

What is timsort?

The timsort npm package is an implementation of the Timsort sorting algorithm for Node.js. Timsort is a hybrid stable sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data. It has been Python's standard sorting algorithm since version 2.3. The npm package allows developers to use this efficient sorting algorithm in their JavaScript projects.

What are timsort's main functionalities?

Sorting an array of numbers

This feature allows you to sort an array of numbers in ascending order using the Timsort algorithm.

const timSort = require('timsort');
let array = [4, 2, 6, 5, 1, 3];
timSort.sort(array);
console.log(array); // Output: [1, 2, 3, 4, 5, 6]

Sorting an array of objects based on a property

This feature allows you to sort an array of objects based on a specific property, in this case by the 'age' property.

const timSort = require('timsort');
let people = [
  { name: 'John', age: 24 },
  { name: 'Alice', age: 30 },
  { name: 'Bob', age: 22 }
];
timSort.sort(people, (a, b) => a.age - b.age);
console.log(people); // Output: [{ name: 'Bob', age: 22 }, { name: 'John', age: 24 }, { name: 'Alice', age: 30 }]

Other packages similar to timsort

Readme

Source

Node-TimSort: Fast Sorting for Node.js

Build Status npm version

An adaptive and stable sort algorithm based on merging that requires fewer than nlog(n) comparisons when run on partially sorted arrays. The algorithm uses O(n) memory and still runs in O(nlogn) (worst case) on random arrays.
This implementation is based on the original TimSort developed by Tim Peters for Python's lists (code here). TimSort has been also adopted in Java starting from version 7.

Acknowledgments

  • @novacrazy: ported the module to ES6/ES7 and made it available via bower
  • @kasperisager: implemented faster lexicographic comparison of small integers

Usage

Install the package with npm:

npm install --save timsort

And use it:

var TimSort = require('timsort');

var arr = [...];
TimSort.sort(arr);

You can also install it with bower:

bower install timsort

As array.sort() by default the timsort module sorts according to lexicographical order. You can also provide your own compare function (to sort any object) as:

function numberCompare(a,b) {
    return a-b;
}

var arr = [...];
var TimSort = require('timsort');
TimSort.sort(arr, numberCompare);

You can also sort only a specific subrange of the array:

TimSort.sort(arr, 5, 10);
TimSort.sort(arr, numberCompare, 5, 10);

Performance

A benchmark is provided in benchmark/index.js. It compares the timsort module against the default array.sort method in the numerical sorting of different types of integer array (as described here):

  • Random array
  • Descending array
  • Ascending array
  • Ascending array with 3 random exchanges
  • Ascending array with 10 random numbers in the end
  • Array of equal elements
  • Random Array with many duplicates
  • Random Array with some duplicates

For any of the array types the sorting is repeated several times and for different array sizes, average execution time is then printed. I run the benchmark on Node v6.3.1 (both pre-compiled and compiled from source, results are very similar), obtaining the following values:

Execution Time (ns)Speedup
Array TypeLengthTimSort.sortarray.sort
Random1040415833.91
100714744420.62
100096395599790.62
10000134104460980654.55
Descending10180188110.41
1006821921028.14
1000380918518548.61
10000358785392428150.30
Ascending101738164.69
1005781814731.34
10002551331993130.12
10000220985382446243.57
Ascending + 3 Rand Exc102329273.99
10010591579214.90
1000352530070885.29
10000274554781370174.15
Ascending + 10 Rand End1037814253.77
10017072334613.67
1000581833474457.53
10000380344985473131.08
Equal Elements101647664.68
10052031886.12
100023402797111.95
100001701128167216.56
Many Repetitions1039614823.74
1007282252673.47
10001055284201203.98
10000139612057873994.15
Some Repetitions1039014633.75
1006678200823.01
10001043443741033.59
10000133381654740004.10

TimSort.sort is faster than array.sort on almost of the tested array types. In general, the more ordered the array is the better TimSort.sort performs with respect to array.sort (up to 243 times faster on already sorted arrays). And also, in general, the bigger the array the more we benefit from using the timsort module.

These data strongly depend on Node.js version and the machine on which the benchmark is run. I strongly encourage you to run the benchmark on your own setup with:

npm run benchmark

Please also notice that:

  • This benchmark is far from exhaustive. Several cases are not considered and the results must be taken as partial
  • inlining is surely playing an active role in timsort module's good performance
  • A more accurate comparison of the algorithms would require implementing array.sort in pure javascript and counting element comparisons

Stability

TimSort is stable which means that equal items maintain their relative order after sorting. Stability is a desirable property for a sorting algorithm. Consider the following array of items with an height and a weight.

[ 
  { height: 100, weight: 80 },
  { height: 90, weight: 90 },
  { height: 70, weight: 95 },
  { height: 100, weight: 100 },
  { height: 80, weight: 110 },
  { height: 110, weight: 115 },
  { height: 100, weight: 120 },
  { height: 70, weight: 125 },
  { height: 70, weight: 130 },
  { height: 100, weight: 135 },
  { height: 75, weight: 140 },
  { height: 70, weight: 140 } 
]

Items are already sorted by weight. Sorting the array according to the item's height with the timsort module results in the following array:

[ 
  { height: 70, weight: 95 },
  { height: 70, weight: 125 },
  { height: 70, weight: 130 },
  { height: 70, weight: 140 },
  { height: 75, weight: 140 },
  { height: 80, weight: 110 },
  { height: 90, weight: 90 },
  { height: 100, weight: 80 },
  { height: 100, weight: 100 },
  { height: 100, weight: 120 },
  { height: 100, weight: 135 },
  { height: 110, weight: 115 } 
]

Items with the same height are still sorted by weight which means they preserved their relative order.

array.sort, instead, is not guarranteed to be stable. In Node v0.12.7 sorting the previous array by height with array.sort results in:

[ 
  { height: 70, weight: 140 },
  { height: 70, weight: 95 },
  { height: 70, weight: 125 },
  { height: 70, weight: 130 },
  { height: 75, weight: 140 },
  { height: 80, weight: 110 },
  { height: 90, weight: 90 },
  { height: 100, weight: 100 },
  { height: 100, weight: 80 },
  { height: 100, weight: 135 },
  { height: 100, weight: 120 },
  { height: 110, weight: 115 } 
]

As you can see the sorting did not preserve weight ordering for items with the same height.

Keywords

FAQs

Last updated on 24 Jul 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc