Code Monkey home page Code Monkey logo

node-timsort's Introduction

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.

Usage

Install the package with npm:

npm install --save timsort

And use it:

var TimSort = require('timsort');

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

Thanks to @novacrazy you can also install the package with bower by running:

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 v0.12.7 (both pre-compiled and compiled from source, results are very similar), obtaining the following values:

Execution Time (ns) Speedup
Array Type Length TimSort.sort array.sort
Random1095841954.38
10010969496564.53
10001464105402543.69
10000175477061438063.50
Descending1064224143.76
10017472071011.85
1000805335168243.67
1000069162510643473.83
Ascending1070614712.08
10017122028911.85
1000849836912543.43
1000059206533331490.08
Ascending + 3 Rand Exc 10 918 1886 2.05
100 3316 21581 6.51
1000 11432 380742 33.30
10000 83981 5293251 63.03
Ascending + 10 Rand End10103721542.08
1004779226584.74
10001660835263321.23
10000102818522533050.82
Equal Elements1074014731.99
100176845762.59
10006934336764.86
10000575863381545.87
Many Repetitions10104421102.02
10012506233911.87
10001841843868962.10
10000264720957980972.19
Some Repetitions10107521832.03
10012787236541.85
10001777273621352.04
10000233244450593512.17

TimSort.sort is faster than array.sort on any 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 80 times faster on already sorted arrays). And also, 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
  • array.sort will probably still be faster at lexicographically sorting arrays of numbers. In this case, the timsort module inefficiently converts values to strings inside the compare function and then compares the strings. array.sort, instead, uses a smarter and faster lexicographic comparison of numbers (will try to do something similar soon).

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.

node-timsort's People

Contributors

mziccard avatar novacrazy avatar

Watchers

James Cloos avatar dieface avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.