{"_id":"array-timsort","_rev":"181290","name":"array-timsort","description":"Fast JavaScript array sorting by implementing Python's Timsort algorithm","dist-tags":{"latest":"1.0.3"},"maintainers":[{"name":"kael","email":"i@kael.me"}],"time":{"modified":"2021-06-03T12:05:06.000Z","created":"2020-10-01T15:33:58.270Z","1.0.3":"2020-10-01T15:43:16.437Z","1.0.2":"2020-10-01T15:41:57.440Z","1.0.1":"2020-10-01T15:37:26.610Z","1.0.0":"2020-10-01T15:33:58.270Z"},"users":{},"repository":{"type":"git","url":"git+https://github.com/kaelzhang/node-array-timsort.git"},"versions":{"1.0.3":{"name":"array-timsort","version":"1.0.3","description":"Fast JavaScript array sorting by implementing Python's Timsort algorithm","homepage":"https://github.com/kaelzhang/node-array-timsort","main":"./src","dependencies":{},"devDependencies":{"@ostai/eslint-config":"^3.5.0","ava":"^3.13.0","codecov":"^3.7.2","eslint":"^7.10.0","eslint-plugin-import":"^2.22.1","eslint-plugin-mocha":"^8.0.0","nyc":"^15.1.0"},"scripts":{"benchmark":"node benchmark/index.js","test":"npm run test:only","test:only":"NODE_DEBUG=array-timsort nyc ava --timeout=10s --verbose","test:dev":"npm run test:only && npm run report:dev","lint":"eslint .","fix":"eslint . --fix","posttest":"npm run report","report":"nyc report --reporter=text-lcov > coverage.lcov && codecov","report:dev":"nyc report --reporter=html && npm run report:open","report:open":"open coverage/index.html"},"ava":{"files":["test/*.test.js"]},"repository":{"type":"git","url":"git+https://github.com/kaelzhang/node-array-timsort.git"},"keywords":["fast sort","array soft","sort","compare","TimSort","algorithm","python","performance"],"license":"MIT","bugs":{"url":"https://github.com/kaelzhang/node-array-timsort/issues"},"gitHead":"8c1c41202078e6702751b8da53b3426621becd6e","_id":"array-timsort@1.0.3","_nodeVersion":"14.12.0","_npmVersion":"6.14.8","dist":{"shasum":"3c9e4199e54fb2b9c3fe5976396a21614ef0d926","size":9169,"noattachment":false,"key":"/array-timsort/-/array-timsort-1.0.3.tgz","tarball":"http://registry.cnpm.dingdandao.com/array-timsort/download/array-timsort-1.0.3.tgz"},"maintainers":[{"name":"kael","email":"i@kael.me"}],"_npmUser":{"name":"kael","email":"i@kael.me"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/array-timsort_1.0.3_1601566996267_0.20647733079624953"},"_hasShrinkwrap":false,"publish_time":1601566996437,"_cnpm_publish_time":1601566996437},"1.0.2":{"name":"array-timsort","version":"1.0.2","description":"Fast JavaScript array sorting by implementing Python's Timsort algorithm","homepage":"https://github.com/kaelzhang/node-array-timsort","main":"./src","dependencies":{},"devDependencies":{"@ostai/eslint-config":"^3.5.0","ava":"^3.13.0","codecov":"^3.7.2","eslint":"^7.10.0","eslint-plugin-import":"^2.22.1","eslint-plugin-mocha":"^8.0.0","nyc":"^15.1.0"},"scripts":{"benchmark":"node benchmark/index.js","test":"npm run test:only","test:only":"NODE_DEBUG=array-timsort nyc ava --timeout=10s --verbose","test:dev":"npm run test:only && npm run report:dev","lint":"eslint .","fix":"eslint . --fix","posttest":"npm run report","report":"nyc report --reporter=text-lcov > coverage.lcov && codecov","report:dev":"nyc report --reporter=html && npm run report:open","report:open":"open coverage/index.html"},"ava":{"files":["test/*.test.js"]},"repository":{"type":"git","url":"git+https://github.com/kaelzhang/node-array-timsort.git"},"keywords":["fast sort","array soft","sort","compare","TimSort","algorithm","python","performance"],"license":"MIT","bugs":{"url":"https://github.com/kaelzhang/node-array-timsort/issues"},"gitHead":"f777f80b6b5bc5a35564e4cc1dec4d9db0aba485","_id":"array-timsort@1.0.2","_nodeVersion":"14.12.0","_npmVersion":"6.14.8","dist":{"shasum":"55262f7e581fecde0547d6ed75da0bef299e1a8f","size":9173,"noattachment":false,"key":"/array-timsort/-/array-timsort-1.0.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/array-timsort/download/array-timsort-1.0.2.tgz"},"maintainers":[{"name":"kael","email":"i@kael.me"}],"_npmUser":{"name":"kael","email":"i@kael.me"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/array-timsort_1.0.2_1601566917289_0.9637300103320794"},"_hasShrinkwrap":false,"publish_time":1601566917440,"_cnpm_publish_time":1601566917440},"1.0.1":{"name":"array-timsort","version":"1.0.1","description":"Fast JavaScript array sorting by implementing Python's Timsort algorithm","homepage":"https://github.com/kaelzhang/node-array-timsort","main":"./src","dependencies":{},"devDependencies":{"@ostai/eslint-config":"^3.5.0","ava":"^3.13.0","codecov":"^3.7.2","eslint":"^7.10.0","eslint-plugin-import":"^2.22.1","eslint-plugin-mocha":"^8.0.0","nyc":"^15.1.0"},"scripts":{"benchmark":"node benchmark/index.js","test":"npm run test:only","test:only":"NODE_DEBUG=array-timsort nyc ava --timeout=10s --verbose","test:dev":"npm run test:only && npm run report:dev","lint":"eslint .","fix":"eslint . --fix","posttest":"npm run report","report":"nyc report --reporter=text-lcov > coverage.lcov && codecov","report:dev":"nyc report --reporter=html && npm run report:open","report:open":"open coverage/index.html"},"ava":{"files":["test/*.test.js"]},"repository":{"type":"git","url":"git+https://github.com/kaelzhang/node-array-timsort.git"},"keywords":["fast sort","array soft","sort","compare","TimSort","algorithm","python","performance"],"license":"MIT","bugs":{"url":"https://github.com/kaelzhang/node-array-timsort/issues"},"gitHead":"8afa7c439f8d4336363920e0901b99b7db027768","_id":"array-timsort@1.0.1","_nodeVersion":"14.12.0","_npmVersion":"6.14.8","dist":{"shasum":"76eee99cca379d2313e2a6828af7f96ce84733a1","size":9098,"noattachment":false,"key":"/array-timsort/-/array-timsort-1.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/array-timsort/download/array-timsort-1.0.1.tgz"},"maintainers":[{"name":"kael","email":"i@kael.me"}],"_npmUser":{"name":"kael","email":"i@kael.me"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/array-timsort_1.0.1_1601566646500_0.8949436331031788"},"_hasShrinkwrap":false,"publish_time":1601566646610,"_cnpm_publish_time":1601566646610},"1.0.0":{"name":"array-timsort","version":"1.0.0","description":"TimSort: Fast Sorting for Node.js","homepage":"https://github.com/kaelzhang/node-array-timsort","main":"./src","dependencies":{},"devDependencies":{"@ostai/eslint-config":"^3.5.0","ava":"^3.13.0","codecov":"^3.7.2","eslint":"^7.10.0","eslint-plugin-import":"^2.22.1","eslint-plugin-mocha":"^8.0.0","nyc":"^15.1.0"},"scripts":{"benchmark":"node benchmark/index.js","test":"npm run test:only","test:only":"NODE_DEBUG=array-timsort nyc ava --timeout=10s --verbose","test:dev":"npm run test:only && npm run report:dev","lint":"eslint .","fix":"eslint . --fix","posttest":"npm run report","report":"nyc report --reporter=text-lcov > coverage.lcov && codecov","report:dev":"nyc report --reporter=html && npm run report:open","report:open":"open coverage/index.html"},"ava":{"files":["test/*.test.js"]},"repository":{"type":"git","url":"git+https://github.com/kaelzhang/node-array-timsort.git"},"keywords":["fast sort","array soft","sort","compare","TimSort","algorithm","python","performance"],"license":"MIT","bugs":{"url":"https://github.com/kaelzhang/node-array-timsort/issues"},"gitHead":"afd38f9fe89303b73d38a45df5f4e8482399e218","_id":"array-timsort@1.0.0","_nodeVersion":"14.12.0","_npmVersion":"6.14.8","dist":{"shasum":"e03fda46dc9457e1dfcd5dd60b368fb032cfe96c","size":9095,"noattachment":false,"key":"/array-timsort/-/array-timsort-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/array-timsort/download/array-timsort-1.0.0.tgz"},"maintainers":[{"name":"kael","email":"i@kael.me"}],"_npmUser":{"name":"kael","email":"i@kael.me"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/array-timsort_1.0.0_1601566438153_0.9820056378245179"},"_hasShrinkwrap":false,"publish_time":1601566438270,"_cnpm_publish_time":1601566438270}},"readme":"[![Build Status](https://travis-ci.org/kaelzhang/node-array-timsort.svg?branch=master)](https://travis-ci.org/kaelzhang/node-array-timsort)\n[![Coverage](https://codecov.io/gh/kaelzhang/node-array-timsort/branch/master/graph/badge.svg)](https://codecov.io/gh/kaelzhang/node-array-timsort)\n\n# array-timsort\n\nA fork of [`timsort`](https://npmjs.org/package/timsort) with the following differences:\n\n- `array-timsort` returns an array which records how the index of items have been sorted, while `timsort` returns `undefined`. See the example below.\n- improves test coverage\n- removes some dead code branches that could never be reached\n- no longer built with UMD\n\n```js\nconst {sort} = require('array-timsort')\n\nconst array = [3, 2, 1, 5]\n\nsort(array)  // returns [2, 1, 0, 4]\n\nconsole.log(array)  // [1, 2, 3, 5]\n```\n\n****\n\nAn adaptive and **stable** sort algorithm based on merging that requires fewer than nlog(n)\ncomparisons when run on partially sorted arrays. The algorithm uses O(n) memory and still runs in O(nlogn)\n(worst case) on random arrays.\nThis implementation is based on the original\n[TimSort](http://svn.python.org/projects/python/trunk/Objects/listsort.txt) developed\nby Tim Peters for Python's lists (code [here](http://svn.python.org/projects/python/trunk/Objects/listobject.c)).\nTimSort has been also adopted in Java starting from version 7.\n\n## Acknowledgments\n\n- @novacrazy: ported the module to ES6/ES7 and made it available via bower\n- @kasperisager: implemented faster lexicographic comparison of small integers\n\n## Usage\n\nInstall the package with npm:\n\n```sh\nnpm i array-timsort\n```\n\nAnd use it:\n\n```js\nconst {sort} = require('array-timsort')\n\nconst arr = [...]\n\nsort(arr)\n```\n\nAs `array.sort()` by default the `array-timsort` module sorts according to\nlexicographical order.\n\nYou can also provide your own compare function (to sort any object) as:\n\n```js\nfunction numberCompare (a, b) {\n    return a - b\n}\n\nconst arr = [...]\n\nsort(arr, numberCompare)\n```\n\nYou can also sort only a specific subrange of the array:\n\n```js\nsort(arr, 5, 10)\nsort(arr, numberCompare, 5, 10)\n```\n\n## Performance\n\nA benchmark is provided in `benchmark/index.js`. It compares the `array-timsort` module against\nthe default `array.sort` method in the numerical sorting of different types of integer array\n(as described [here](http://svn.python.org/projects/python/trunk/Objects/listsort.txt)):\n\n-  *Random array*\n-  *Descending array*\n-  *Ascending array*\n-  *Ascending array with 3 random exchanges*\n-  *Ascending array with 10 random numbers in the end*\n-  *Array of equal elements*\n-  *Random Array with many duplicates*\n-  *Random Array with some duplicates*\n\nFor any of the array types the sorting is repeated several times and for\ndifferent array sizes, average execution time is then printed.\nI run the benchmark on Node v6.3.1 (both pre-compiled and compiled from source,\nresults are very similar), obtaining the following values:\n\n<table>\n  <tr>\n    <th></th><th></th>\n    <th colspan=\"2\">Execution Time (ns)</th>\n    <th rowspan=\"2\">Speedup</th>\n  </tr>\n  <tr>\n    <th>Array Type</th>\n    <th>Length</th>\n    <th>TimSort.sort</th>\n    <th>array.sort</th>\n  </tr>\n<tbody>\n <tr>\n  <td rowspan=\"4\">Random</td><td>10</td><td>404</td><td>1583</td><td>3.91</td>\n </tr>\n <tr>\n  <td>100</td><td>7147</td><td>4442</td><td>0.62</td>\n </tr>\n <tr>\n  <td>1000</td><td>96395</td><td>59979</td><td>0.62</td>\n </tr>\n <tr>\n  <td>10000</td><td>1341044</td><td>6098065</td><td>4.55</td>\n </tr>\n <tr>\n  <td rowspan=\"4\">Descending</td><td>10</td><td>180</td><td>1881</td><td>10.41</td>\n </tr>\n <tr>\n  <td>100</td><td>682</td><td>19210</td><td>28.14</td>\n</tr>\n <tr>\n  <td>1000</td><td>3809</td><td>185185</td><td>48.61</td>\n </tr>\n <tr>\n  <td>10000</td><td>35878</td><td>5392428</td><td>150.30</td>\n </tr>\n <tr>\n  <td rowspan=\"4\">Ascending</td><td>10</td><td>173</td><td>816</td><td>4.69</td>\n </tr>\n <tr>\n  <td>100</td><td>578</td><td>18147</td><td>31.34</td>\n </tr>\n <tr>\n  <td>1000</td><td>2551</td><td>331993</td><td>130.12</td>\n </tr>\n <tr>\n  <td>10000</td><td>22098</td><td>5382446</td><td>243.57</td>\n </tr>\n <tr>\n  <td rowspan=\"4\">Ascending + 3 Rand Exc</td><td>10</td><td>232</td><td>927</td><td>3.99</td>\n </tr>\n <tr>\n  <td>100</td><td>1059</td><td>15792</td><td>14.90</td>\n </tr>\n <tr>\n  <td>1000</td><td>3525</td><td>300708</td><td>85.29</td>\n </tr>\n <tr>\n  <td>10000</td><td>27455</td><td>4781370</td><td>174.15</td>\n </tr>\n <tr>\n  <td rowspan=\"4\">Ascending + 10 Rand End</td><td>10</td><td>378</td><td>1425</td><td>3.77</td>\n </tr>\n <tr>\n  <td>100</td><td>1707</td><td>23346</td><td>13.67</td>\n </tr>\n <tr>\n  <td>1000</td><td>5818</td><td>334744</td><td>57.53</td>\n </tr>\n <tr>\n  <td>10000</td><td>38034</td><td>4985473</td><td>131.08</td>\n </tr>\n <tr>\n  <td rowspan=\"4\">Equal Elements</td><td>10</td><td>164</td><td>766</td><td>4.68</td>\n </tr>\n <tr>\n  <td>100</td><td>520</td><td>3188</td><td>6.12</td>\n </tr>\n <tr>\n  <td>1000</td><td>2340</td><td>27971</td><td>11.95</td>\n </tr>\n <tr>\n  <td>10000</td><td>17011</td><td>281672</td><td>16.56</td>\n </tr>\n <tr>\n  <td rowspan=\"4\">Many Repetitions</td><td>10</td><td>396</td><td>1482</td><td>3.74</td>\n </tr>\n <tr>\n  <td>100</td><td>7282</td><td>25267</td><td>3.47</td>\n </tr>\n <tr>\n  <td>1000</td><td>105528</td><td>420120</td><td>3.98</td>\n </tr>\n <tr>\n  <td>10000</td><td>1396120</td><td>5787399</td><td>4.15</td>\n </tr>\n <tr>\n  <td rowspan=\"4\">Some Repetitions</td><td>10</td><td>390</td><td>1463</td><td>3.75</td>\n </tr>\n <tr>\n  <td>100</td><td>6678</td><td>20082</td><td>3.01</td>\n </tr>\n <tr>\n  <td>1000</td><td>104344</td><td>374103</td><td>3.59</td>\n </tr>\n <tr>\n  <td>10000</td><td>1333816</td><td>5474000</td><td>4.10</td>\n </tr>\n</tbody>\n</table>\n\n`TimSort.sort` **is faster** than `array.sort` on almost of the tested array types.\nIn 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).\nAnd also, in general, the bigger the array the more we benefit from using\nthe `array-timsort` module.\n\nThese 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:\n```\nnpm run benchmark\n```\nPlease also notice that:\n\n-  This benchmark is far from exhaustive. Several cases are not considered\nand the results must be taken as partial\n-  *inlining* is surely playing an active role in `array-timsort` module's good performance\n-  A more accurate comparison of the algorithms would require implementing `array.sort` in pure javascript\nand counting element comparisons\n\n## Stability\n\nTimSort is *stable* which means that equal items maintain their relative order\nafter sorting. Stability is a desirable property for a sorting algorithm.\nConsider the following array of items with an height and a weight.\n```javascript\n[\n  { height: 100, weight: 80 },\n  { height: 90, weight: 90 },\n  { height: 70, weight: 95 },\n  { height: 100, weight: 100 },\n  { height: 80, weight: 110 },\n  { height: 110, weight: 115 },\n  { height: 100, weight: 120 },\n  { height: 70, weight: 125 },\n  { height: 70, weight: 130 },\n  { height: 100, weight: 135 },\n  { height: 75, weight: 140 },\n  { height: 70, weight: 140 }\n]\n```\nItems are already sorted by `weight`. Sorting the array\naccording to the item's `height` with the `array-timsort` module\nresults in the following array:\n```javascript\n[\n  { height: 70, weight: 95 },\n  { height: 70, weight: 125 },\n  { height: 70, weight: 130 },\n  { height: 70, weight: 140 },\n  { height: 75, weight: 140 },\n  { height: 80, weight: 110 },\n  { height: 90, weight: 90 },\n  { height: 100, weight: 80 },\n  { height: 100, weight: 100 },\n  { height: 100, weight: 120 },\n  { height: 100, weight: 135 },\n  { height: 110, weight: 115 }\n]\n```\nItems with the same  `height` are still sorted by `weight` which means they preserved their relative order.\n\n`array.sort`, instead, is not guarranteed to be *stable*. In Node v0.12.7\nsorting the previous array by `height` with `array.sort` results in:\n```javascript\n[\n  { height: 70, weight: 140 },\n  { height: 70, weight: 95 },\n  { height: 70, weight: 125 },\n  { height: 70, weight: 130 },\n  { height: 75, weight: 140 },\n  { height: 80, weight: 110 },\n  { height: 90, weight: 90 },\n  { height: 100, weight: 100 },\n  { height: 100, weight: 80 },\n  { height: 100, weight: 135 },\n  { height: 100, weight: 120 },\n  { height: 110, weight: 115 }\n]\n```\nAs you can see the sorting did not preserve `weight` ordering for items with the\nsame `height`.\n","_attachments":{},"homepage":"https://github.com/kaelzhang/node-array-timsort","bugs":{"url":"https://github.com/kaelzhang/node-array-timsort/issues"},"license":"MIT"}