You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
7.9 KiB

4 years ago
  1. # Node-TimSort: Fast Sorting for Node.js
  2. [![Build Status](https://travis-ci.org/mziccard/node-timsort.svg?branch=master)](https://travis-ci.org/mziccard/node-timsort)
  3. [![npm version](https://badge.fury.io/js/timsort.svg)](https://www.npmjs.com/package/timsort)
  4. An adaptive and **stable** sort algorithm based on merging that requires fewer than nlog(n)
  5. comparisons when run on partially sorted arrays. The algorithm uses O(n) memory and still runs in O(nlogn)
  6. (worst case) on random arrays.
  7. This implementation is based on the original
  8. [TimSort](http://svn.python.org/projects/python/trunk/Objects/listsort.txt) developed
  9. by Tim Peters for Python's lists (code [here](http://svn.python.org/projects/python/trunk/Objects/listobject.c)).
  10. TimSort has been also adopted in Java starting from version 7.
  11. ## Acknowledgments
  12. - @novacrazy: ported the module to ES6/ES7 and made it available via bower
  13. - @kasperisager: implemented faster lexicographic comparison of small integers
  14. ## Usage
  15. Install the package with npm:
  16. ```
  17. npm install --save timsort
  18. ```
  19. And use it:
  20. ```javascript
  21. var TimSort = require('timsort');
  22. var arr = [...];
  23. TimSort.sort(arr);
  24. ```
  25. You can also install it with bower:
  26. ```
  27. bower install timsort
  28. ```
  29. As `array.sort()` by default the `timsort` module sorts according to
  30. lexicographical order.
  31. You can also provide your own compare function (to sort any object) as:
  32. ```javascript
  33. function numberCompare(a,b) {
  34. return a-b;
  35. }
  36. var arr = [...];
  37. var TimSort = require('timsort');
  38. TimSort.sort(arr, numberCompare);
  39. ```
  40. You can also sort only a specific subrange of the array:
  41. ```javascript
  42. TimSort.sort(arr, 5, 10);
  43. TimSort.sort(arr, numberCompare, 5, 10);
  44. ```
  45. ## Performance
  46. A benchmark is provided in `benchmark/index.js`. It compares the `timsort` module against
  47. the default `array.sort` method in the numerical sorting of different types of integer array
  48. (as described [here](http://svn.python.org/projects/python/trunk/Objects/listsort.txt)):
  49. - *Random array*
  50. - *Descending array*
  51. - *Ascending array*
  52. - *Ascending array with 3 random exchanges*
  53. - *Ascending array with 10 random numbers in the end*
  54. - *Array of equal elements*
  55. - *Random Array with many duplicates*
  56. - *Random Array with some duplicates*
  57. For any of the array types the sorting is repeated several times and for
  58. different array sizes, average execution time is then printed.
  59. I run the benchmark on Node v6.3.1 (both pre-compiled and compiled from source,
  60. results are very similar), obtaining the following values:
  61. <table>
  62. <tr>
  63. <th></th><th></th>
  64. <th colspan="2">Execution Time (ns)</th>
  65. <th rowspan="2">Speedup</th>
  66. </tr>
  67. <tr>
  68. <th>Array Type</th>
  69. <th>Length</th>
  70. <th>TimSort.sort</th>
  71. <th>array.sort</th>
  72. </tr>
  73. <tbody>
  74. <tr>
  75. <td rowspan="4">Random</td><td>10</td><td>404</td><td>1583</td><td>3.91</td>
  76. </tr>
  77. <tr>
  78. <td>100</td><td>7147</td><td>4442</td><td>0.62</td>
  79. </tr>
  80. <tr>
  81. <td>1000</td><td>96395</td><td>59979</td><td>0.62</td>
  82. </tr>
  83. <tr>
  84. <td>10000</td><td>1341044</td><td>6098065</td><td>4.55</td>
  85. </tr>
  86. <tr>
  87. <td rowspan="4">Descending</td><td>10</td><td>180</td><td>1881</td><td>10.41</td>
  88. </tr>
  89. <tr>
  90. <td>100</td><td>682</td><td>19210</td><td>28.14</td>
  91. </tr>
  92. <tr>
  93. <td>1000</td><td>3809</td><td>185185</td><td>48.61</td>
  94. </tr>
  95. <tr>
  96. <td>10000</td><td>35878</td><td>5392428</td><td>150.30</td>
  97. </tr>
  98. <tr>
  99. <td rowspan="4">Ascending</td><td>10</td><td>173</td><td>816</td><td>4.69</td>
  100. </tr>
  101. <tr>
  102. <td>100</td><td>578</td><td>18147</td><td>31.34</td>
  103. </tr>
  104. <tr>
  105. <td>1000</td><td>2551</td><td>331993</td><td>130.12</td>
  106. </tr>
  107. <tr>
  108. <td>10000</td><td>22098</td><td>5382446</td><td>243.57</td>
  109. </tr>
  110. <tr>
  111. <td rowspan="4">Ascending + 3 Rand Exc</td><td>10</td><td>232</td><td>927</td><td>3.99</td>
  112. </tr>
  113. <tr>
  114. <td>100</td><td>1059</td><td>15792</td><td>14.90</td>
  115. </tr>
  116. <tr>
  117. <td>1000</td><td>3525</td><td>300708</td><td>85.29</td>
  118. </tr>
  119. <tr>
  120. <td>10000</td><td>27455</td><td>4781370</td><td>174.15</td>
  121. </tr>
  122. <tr>
  123. <td rowspan="4">Ascending + 10 Rand End</td><td>10</td><td>378</td><td>1425</td><td>3.77</td>
  124. </tr>
  125. <tr>
  126. <td>100</td><td>1707</td><td>23346</td><td>13.67</td>
  127. </tr>
  128. <tr>
  129. <td>1000</td><td>5818</td><td>334744</td><td>57.53</td>
  130. </tr>
  131. <tr>
  132. <td>10000</td><td>38034</td><td>4985473</td><td>131.08</td>
  133. </tr>
  134. <tr>
  135. <td rowspan="4">Equal Elements</td><td>10</td><td>164</td><td>766</td><td>4.68</td>
  136. </tr>
  137. <tr>
  138. <td>100</td><td>520</td><td>3188</td><td>6.12</td>
  139. </tr>
  140. <tr>
  141. <td>1000</td><td>2340</td><td>27971</td><td>11.95</td>
  142. </tr>
  143. <tr>
  144. <td>10000</td><td>17011</td><td>281672</td><td>16.56</td>
  145. </tr>
  146. <tr>
  147. <td rowspan="4">Many Repetitions</td><td>10</td><td>396</td><td>1482</td><td>3.74</td>
  148. </tr>
  149. <tr>
  150. <td>100</td><td>7282</td><td>25267</td><td>3.47</td>
  151. </tr>
  152. <tr>
  153. <td>1000</td><td>105528</td><td>420120</td><td>3.98</td>
  154. </tr>
  155. <tr>
  156. <td>10000</td><td>1396120</td><td>5787399</td><td>4.15</td>
  157. </tr>
  158. <tr>
  159. <td rowspan="4">Some Repetitions</td><td>10</td><td>390</td><td>1463</td><td>3.75</td>
  160. </tr>
  161. <tr>
  162. <td>100</td><td>6678</td><td>20082</td><td>3.01</td>
  163. </tr>
  164. <tr>
  165. <td>1000</td><td>104344</td><td>374103</td><td>3.59</td>
  166. </tr>
  167. <tr>
  168. <td>10000</td><td>1333816</td><td>5474000</td><td>4.10</td>
  169. </tr>
  170. </tbody>
  171. </table>
  172. `TimSort.sort` **is faster** than `array.sort` on almost of the tested array types.
  173. 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).
  174. And also, in general, the bigger the array the more we benefit from using
  175. the `timsort` module.
  176. 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:
  177. ```
  178. npm run benchmark
  179. ```
  180. Please also notice that:
  181. - This benchmark is far from exhaustive. Several cases are not considered
  182. and the results must be taken as partial
  183. - *inlining* is surely playing an active role in `timsort` module's good performance
  184. - A more accurate comparison of the algorithms would require implementing `array.sort` in pure javascript
  185. and counting element comparisons
  186. ## Stability
  187. TimSort is *stable* which means that equal items maintain their relative order
  188. after sorting. Stability is a desirable property for a sorting algorithm.
  189. Consider the following array of items with an height and a weight.
  190. ```javascript
  191. [
  192. { height: 100, weight: 80 },
  193. { height: 90, weight: 90 },
  194. { height: 70, weight: 95 },
  195. { height: 100, weight: 100 },
  196. { height: 80, weight: 110 },
  197. { height: 110, weight: 115 },
  198. { height: 100, weight: 120 },
  199. { height: 70, weight: 125 },
  200. { height: 70, weight: 130 },
  201. { height: 100, weight: 135 },
  202. { height: 75, weight: 140 },
  203. { height: 70, weight: 140 }
  204. ]
  205. ```
  206. Items are already sorted by `weight`. Sorting the array
  207. according to the item's `height` with the `timsort` module
  208. results in the following array:
  209. ```javascript
  210. [
  211. { height: 70, weight: 95 },
  212. { height: 70, weight: 125 },
  213. { height: 70, weight: 130 },
  214. { height: 70, weight: 140 },
  215. { height: 75, weight: 140 },
  216. { height: 80, weight: 110 },
  217. { height: 90, weight: 90 },
  218. { height: 100, weight: 80 },
  219. { height: 100, weight: 100 },
  220. { height: 100, weight: 120 },
  221. { height: 100, weight: 135 },
  222. { height: 110, weight: 115 }
  223. ]
  224. ```
  225. Items with the same `height` are still sorted by `weight` which means they preserved their relative order.
  226. `array.sort`, instead, is not guarranteed to be *stable*. In Node v0.12.7
  227. sorting the previous array by `height` with `array.sort` results in:
  228. ```javascript
  229. [
  230. { height: 70, weight: 140 },
  231. { height: 70, weight: 95 },
  232. { height: 70, weight: 125 },
  233. { height: 70, weight: 130 },
  234. { height: 75, weight: 140 },
  235. { height: 80, weight: 110 },
  236. { height: 90, weight: 90 },
  237. { height: 100, weight: 100 },
  238. { height: 100, weight: 80 },
  239. { height: 100, weight: 135 },
  240. { height: 100, weight: 120 },
  241. { height: 110, weight: 115 }
  242. ]
  243. ```
  244. As you can see the sorting did not preserve `weight` ordering for items with the
  245. same `height`.