{"action":"created","comment":{"url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/pulls/comments/175416537","pull_request_review_id":104939517,"id":175416537,"diff_hunk":"@@ -2120,24 +2124,41 @@\n   //. Cons('red', Cons('blue', Cons('green', Nil)))\n   //. ```\n   function sortBy(f, foldable) {\n-    var rs = reduce(function(xs, x) {\n-      var fx = f(x);\n-      var lower = 0;\n-      var upper = xs.length;\n-      while (lower < upper) {\n-        var idx = Math.floor((lower + upper) / 2);\n-        if (lte(xs[idx].fx, fx)) lower = idx + 1; else upper = idx;\n-      }\n-      xs.splice(lower, 0, {x: x, fx: fx});\n+    var foldableIsArray = Array.isArray(foldable);\n+    var idx = 0;\n+    function reducer(xs, x) {\n+      xs.push({idx: idx, x: x, prop: f(x)});\n+      idx += 1;\n       return xs;\n-    }, [], foldable);\n-\n-    var F = foldable.constructor;\n-    var result = empty(F);\n-    for (var idx = 0; idx < rs.length; idx += 1) {\n-      result = concat(result, of(F, rs[idx].x));\n     }\n-    return result;\n+    var rs = reduce(reducer, [], foldable);\n+    if (rs.length === 0) {\n+      return foldable;\n+    }\n+    var lteFn =\n+      typeof rs[0].prop === 'string' || typeof rs[0].prop === 'number' ?\n+        function(a, b) { return a <= b; } :\n+        lte;\n+    rs.sort(function(a, b) {\n+      return lteFn(a.prop, b.prop) ?\n+        lteFn(b.prop, a.prop) ? a.idx - b.idx : -1 :\n+        1;\n+    });\n+    if (foldableIsArray) {","path":"index.js","position":53,"original_position":53,"commit_id":"c4c92db0853d50c29f203ad94a4647997997efcc","original_commit_id":"c4c92db0853d50c29f203ad94a4647997997efcc","user":{"login":"davidchambers","id":210406,"avatar_url":"https://avatars2.githubusercontent.com/u/210406?v=4","gravatar_id":"","url":"https://api.github.com/users/davidchambers","html_url":"https://github.com/davidchambers","followers_url":"https://api.github.com/users/davidchambers/followers","following_url":"https://api.github.com/users/davidchambers/following{/other_user}","gists_url":"https://api.github.com/users/davidchambers/gists{/gist_id}","starred_url":"https://api.github.com/users/davidchambers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidchambers/subscriptions","organizations_url":"https://api.github.com/users/davidchambers/orgs","repos_url":"https://api.github.com/users/davidchambers/repos","events_url":"https://api.github.com/users/davidchambers/events{/privacy}","received_events_url":"https://api.github.com/users/davidchambers/received_events","type":"User","site_admin":false},"body":"Since `foldableIsArray` is only referenced here, let's replace this identifier with `Array.isArray(foldable)`.\r\n","created_at":"2018-03-19T12:25:54Z","updated_at":"2018-03-19T12:25:54Z","html_url":"https://github.com/sanctuary-js/sanctuary-type-classes/pull/92#discussion_r175416537","pull_request_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/pulls/92","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/pulls/comments/175416537"},"html":{"href":"https://github.com/sanctuary-js/sanctuary-type-classes/pull/92#discussion_r175416537"},"pull_request":{"href":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/pulls/92"}}},"pull_request":{"url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/pulls/92","id":175854324,"html_url":"https://github.com/sanctuary-js/sanctuary-type-classes/pull/92","diff_url":"https://github.com/sanctuary-js/sanctuary-type-classes/pull/92.diff","patch_url":"https://github.com/sanctuary-js/sanctuary-type-classes/pull/92.patch","issue_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/issues/92","number":92,"state":"open","locked":false,"title":"Improve performance of sortBy","user":{"login":"paldepind","id":521604,"avatar_url":"https://avatars1.githubusercontent.com/u/521604?v=4","gravatar_id":"","url":"https://api.github.com/users/paldepind","html_url":"https://github.com/paldepind","followers_url":"https://api.github.com/users/paldepind/followers","following_url":"https://api.github.com/users/paldepind/following{/other_user}","gists_url":"https://api.github.com/users/paldepind/gists{/gist_id}","starred_url":"https://api.github.com/users/paldepind/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paldepind/subscriptions","organizations_url":"https://api.github.com/users/paldepind/orgs","repos_url":"https://api.github.com/users/paldepind/repos","events_url":"https://api.github.com/users/paldepind/events{/privacy}","received_events_url":"https://api.github.com/users/paldepind/received_events","type":"User","site_admin":false},"body":"This PR improves the performance of `sortBy` and thus also `sort`. These are the benchmark results I get when running `sort` on an array of 100 numbers.\r\n\r\n```\r\n┌───────────────────────┬────────────────────┬──────────────────────┬────────┬───────────┬───┐\r\n│ suite                 │ old                │ new                  │ diff   │ change    │ α │\r\n├───────────────────────┼────────────────────┼──────────────────────┼────────┼───────────┼───┤\r\n│ methods.sort.Array    │ 3 Hz ±1.49% (n 12) │ 637 Hz ±0.30% (n 89) │ 099.1% │ +21464.1% │ ✓ │\r\n├───────────────────────┼────────────────────┼──────────────────────┼────────┼───────────┼───┤\r\n│ methods.sort.Foldable │ 4 Hz ±0.99% (n 14) │ 317 Hz ±0.45% (n 82) │ 097.7% │ +8503.4%  │ ✓ │\r\n└───────────────────────┴────────────────────┴──────────────────────┴────────┴───────────┴───┘\r\n```\r\n\r\nIn this case sorting an array got 214 times faster and sorting a foldable (using List as the benchmark subject) got 85 times faster :smile: \r\n\r\nNote however that to achieve the performance increase I had to use fewer sanctuary-type-class functions internally in the implementation of `sortBy`. It seems that using sanctuary-type-class functions results in some overhead. I created two benchmarks to try and measure the overhead of the functions used inside `sortBy`.\r\n```\r\nvar test = R.range(0, 100);\r\n// ...\r\n'methods.test.reduceMin':     function(Z) { Z.reduce(Z.min, 0, test); },\r\n'methods.test.append':        function(Z) { Z.reduce((a, b) => Z.append(b, a), [], [0, 1, 2, 3, 4]); },\r\n```\r\nThen I plugged in Ramda and sanctuary-type-class in the above benchmarks and got the following results:\r\n```\r\n┌───────────────────────┬──────────────────────────┬────────────────────────────┬────────┬───────────┬───┐\r\n│ suite                 │ sanctuary-type-classes   │ Ramda                      │ diff   │ change    │ α │\r\n├───────────────────────┼──────────────────────────┼────────────────────────────┼────────┼───────────┼───┤\r\n│ methods.test.reduceMin│ 2,073 Hz ±0.52% (n 89)   │ 504,551 Hz ±0.37% (n 93)   │ 099.2% │ +24235.2% │ ✓ │\r\n├───────────────────────┼──────────────────────────┼────────────────────────────┼────────┼───────────┼───┤\r\n│ methods.test.append   │ 235,364 Hz ±1.02% (n 87) │ 1,022,981 Hz ±0.46% (n 91) │ 062.6% │ +334.6%   │ ✓ │\r\n└───────────────────────┴──────────────────────────┴────────────────────────────┴────────┴───────────┴───┘\r\n```\r\nGetting the minimum of an array of 100 numbers seems to be 242 times as slow with sanctuary-type-classes and creating an array of size 5 by prepending is about 3.3 times slower than Ramda. `Z.lte` in particular seems to be be slow and it is still used inside `sortBy` in some cases and in these cases `sortBy` is still slow due to `Z.lte` ending up as the bottleneck.","created_at":"2018-03-19T10:14:53Z","updated_at":"2018-03-19T12:25:54Z","closed_at":null,"merged_at":null,"merge_commit_sha":"21545c8901ba0078a504e43d22709d2a91d95828","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/pulls/92/commits","review_comments_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/pulls/92/comments","review_comment_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/pulls/comments{/number}","comments_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/issues/92/comments","statuses_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/statuses/c4c92db0853d50c29f203ad94a4647997997efcc","head":{"label":"paldepind:sort-performance","ref":"sort-performance","sha":"c4c92db0853d50c29f203ad94a4647997997efcc","user":{"login":"paldepind","id":521604,"avatar_url":"https://avatars1.githubusercontent.com/u/521604?v=4","gravatar_id":"","url":"https://api.github.com/users/paldepind","html_url":"https://github.com/paldepind","followers_url":"https://api.github.com/users/paldepind/followers","following_url":"https://api.github.com/users/paldepind/following{/other_user}","gists_url":"https://api.github.com/users/paldepind/gists{/gist_id}","starred_url":"https://api.github.com/users/paldepind/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paldepind/subscriptions","organizations_url":"https://api.github.com/users/paldepind/orgs","repos_url":"https://api.github.com/users/paldepind/repos","events_url":"https://api.github.com/users/paldepind/events{/privacy}","received_events_url":"https://api.github.com/users/paldepind/received_events","type":"User","site_admin":false},"repo":{"id":124899847,"name":"sanctuary-type-classes","full_name":"paldepind/sanctuary-type-classes","owner":{"login":"paldepind","id":521604,"avatar_url":"https://avatars1.githubusercontent.com/u/521604?v=4","gravatar_id":"","url":"https://api.github.com/users/paldepind","html_url":"https://github.com/paldepind","followers_url":"https://api.github.com/users/paldepind/followers","following_url":"https://api.github.com/users/paldepind/following{/other_user}","gists_url":"https://api.github.com/users/paldepind/gists{/gist_id}","starred_url":"https://api.github.com/users/paldepind/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paldepind/subscriptions","organizations_url":"https://api.github.com/users/paldepind/orgs","repos_url":"https://api.github.com/users/paldepind/repos","events_url":"https://api.github.com/users/paldepind/events{/privacy}","received_events_url":"https://api.github.com/users/paldepind/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/paldepind/sanctuary-type-classes","description":":rainbow: Standard library for Fantasy Land","fork":true,"url":"https://api.github.com/repos/paldepind/sanctuary-type-classes","forks_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/forks","keys_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/keys{/key_id}","collaborators_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/teams","hooks_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/hooks","issue_events_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/issues/events{/number}","events_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/events","assignees_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/assignees{/user}","branches_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/branches{/branch}","tags_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/tags","blobs_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/git/refs{/sha}","trees_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/git/trees{/sha}","statuses_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/statuses/{sha}","languages_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/languages","stargazers_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/stargazers","contributors_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/contributors","subscribers_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/subscribers","subscription_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/subscription","commits_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/commits{/sha}","git_commits_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/git/commits{/sha}","comments_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/comments{/number}","issue_comment_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/issues/comments{/number}","contents_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/contents/{+path}","compare_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/compare/{base}...{head}","merges_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/merges","archive_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/downloads","issues_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/issues{/number}","pulls_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/pulls{/number}","milestones_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/milestones{/number}","notifications_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/labels{/name}","releases_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/releases{/id}","deployments_url":"https://api.github.com/repos/paldepind/sanctuary-type-classes/deployments","created_at":"2018-03-12T14:15:29Z","updated_at":"2018-03-12T14:15:31Z","pushed_at":"2018-03-19T09:37:07Z","git_url":"git://github.com/paldepind/sanctuary-type-classes.git","ssh_url":"git@github.com:paldepind/sanctuary-type-classes.git","clone_url":"https://github.com/paldepind/sanctuary-type-classes.git","svn_url":"https://github.com/paldepind/sanctuary-type-classes","homepage":"","size":349,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit"},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"sanctuary-js:master","ref":"master","sha":"b177457366e1cec7d021f3a1f852de3b0c6dd3fc","user":{"login":"sanctuary-js","id":18357498,"avatar_url":"https://avatars1.githubusercontent.com/u/18357498?v=4","gravatar_id":"","url":"https://api.github.com/users/sanctuary-js","html_url":"https://github.com/sanctuary-js","followers_url":"https://api.github.com/users/sanctuary-js/followers","following_url":"https://api.github.com/users/sanctuary-js/following{/other_user}","gists_url":"https://api.github.com/users/sanctuary-js/gists{/gist_id}","starred_url":"https://api.github.com/users/sanctuary-js/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sanctuary-js/subscriptions","organizations_url":"https://api.github.com/users/sanctuary-js/orgs","repos_url":"https://api.github.com/users/sanctuary-js/repos","events_url":"https://api.github.com/users/sanctuary-js/events{/privacy}","received_events_url":"https://api.github.com/users/sanctuary-js/received_events","type":"Organization","site_admin":false},"repo":{"id":58353356,"name":"sanctuary-type-classes","full_name":"sanctuary-js/sanctuary-type-classes","owner":{"login":"sanctuary-js","id":18357498,"avatar_url":"https://avatars1.githubusercontent.com/u/18357498?v=4","gravatar_id":"","url":"https://api.github.com/users/sanctuary-js","html_url":"https://github.com/sanctuary-js","followers_url":"https://api.github.com/users/sanctuary-js/followers","following_url":"https://api.github.com/users/sanctuary-js/following{/other_user}","gists_url":"https://api.github.com/users/sanctuary-js/gists{/gist_id}","starred_url":"https://api.github.com/users/sanctuary-js/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sanctuary-js/subscriptions","organizations_url":"https://api.github.com/users/sanctuary-js/orgs","repos_url":"https://api.github.com/users/sanctuary-js/repos","events_url":"https://api.github.com/users/sanctuary-js/events{/privacy}","received_events_url":"https://api.github.com/users/sanctuary-js/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/sanctuary-js/sanctuary-type-classes","description":":rainbow: Standard library for Fantasy Land","fork":false,"url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes","forks_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/forks","keys_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/keys{/key_id}","collaborators_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/teams","hooks_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/hooks","issue_events_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/issues/events{/number}","events_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/events","assignees_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/assignees{/user}","branches_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/branches{/branch}","tags_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/tags","blobs_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/git/refs{/sha}","trees_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/git/trees{/sha}","statuses_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/statuses/{sha}","languages_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/languages","stargazers_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/stargazers","contributors_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/contributors","subscribers_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/subscribers","subscription_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/subscription","commits_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/commits{/sha}","git_commits_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/git/commits{/sha}","comments_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/comments{/number}","issue_comment_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/issues/comments{/number}","contents_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/contents/{+path}","compare_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/compare/{base}...{head}","merges_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/merges","archive_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/downloads","issues_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/issues{/number}","pulls_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/pulls{/number}","milestones_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/milestones{/number}","notifications_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/labels{/name}","releases_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/releases{/id}","deployments_url":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/deployments","created_at":"2016-05-09T06:24:11Z","updated_at":"2018-03-18T12:17:58Z","pushed_at":"2018-03-19T10:14:54Z","git_url":"git://github.com/sanctuary-js/sanctuary-type-classes.git","ssh_url":"git@github.com:sanctuary-js/sanctuary-type-classes.git","clone_url":"https://github.com/sanctuary-js/sanctuary-type-classes.git","svn_url":"https://github.com/sanctuary-js/sanctuary-type-classes","homepage":"","size":349,"stargazers_count":84,"watchers_count":84,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":10,"mirror_url":null,"archived":false,"open_issues_count":10,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit"},"forks":10,"open_issues":10,"watchers":84,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/pulls/92"},"html":{"href":"https://github.com/sanctuary-js/sanctuary-type-classes/pull/92"},"issue":{"href":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/issues/92"},"comments":{"href":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/issues/92/comments"},"review_comments":{"href":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/pulls/92/comments"},"review_comment":{"href":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/pulls/92/commits"},"statuses":{"href":"https://api.github.com/repos/sanctuary-js/sanctuary-type-classes/statuses/c4c92db0853d50c29f203ad94a4647997997efcc"}},"author_association":"NONE"}}
{"actor":{"display_login":"davidchambers"}}