{"action":"created","issue":{"url":"https://api.github.com/repos/azer/left-pad/issues/11","repository_url":"https://api.github.com/repos/azer/left-pad","labels_url":"https://api.github.com/repos/azer/left-pad/issues/11/labels{/name}","comments_url":"https://api.github.com/repos/azer/left-pad/issues/11/comments","events_url":"https://api.github.com/repos/azer/left-pad/issues/11/events","html_url":"https://github.com/azer/left-pad/pull/11","id":143048382,"number":11,"title":"highly efficient solution","user":{"login":"haoel","id":1014558,"avatar_url":"https://avatars.githubusercontent.com/u/1014558?v=3","gravatar_id":"","url":"https://api.github.com/users/haoel","html_url":"https://github.com/haoel","followers_url":"https://api.github.com/users/haoel/followers","following_url":"https://api.github.com/users/haoel/following{/other_user}","gists_url":"https://api.github.com/users/haoel/gists{/gist_id}","starred_url":"https://api.github.com/users/haoel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/haoel/subscriptions","organizations_url":"https://api.github.com/users/haoel/orgs","repos_url":"https://api.github.com/users/haoel/repos","events_url":"https://api.github.com/users/haoel/events{/privacy}","received_events_url":"https://api.github.com/users/haoel/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":9,"created_at":"2016-03-23T18:49:19Z","updated_at":"2016-03-25T12:24:12Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/azer/left-pad/pulls/11","html_url":"https://github.com/azer/left-pad/pull/11","diff_url":"https://github.com/azer/left-pad/pull/11.diff","patch_url":"https://github.com/azer/left-pad/pull/11.patch"},"body":"Using while-loop to keep adding the padding char one by one is not efficient. \r\n\r\nActually we can do it better. We can use bit operation to keeping doubling the padding chars which can significantly reduce the string concatenation operation.   \r\n\r\nThe final code as below:\r\n```\r\nmodule.exports = leftpad;\r\n\r\nfunction leftpad (str, len, ch) {\r\n  str = String(str);\r\n\r\n  if (!ch && ch !== 0) ch = ' ';\r\n\r\n  len = len - str.length;\r\n  if (len <= 0) return str;\r\n\r\n  ch = ch + '';\r\n  var pad = '';\r\n  while (true) {\r\n    if (len & 1) pad += ch;\r\n    len >>= 1;\r\n    if (len) ch += ch;\r\n    else break;\r\n  }\r\n  return pad + str;\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/azer/left-pad/issues/comments/201261179","html_url":"https://github.com/azer/left-pad/pull/11#issuecomment-201261179","issue_url":"https://api.github.com/repos/azer/left-pad/issues/11","id":201261179,"user":{"login":"yuanweiwu","id":11427457,"avatar_url":"https://avatars.githubusercontent.com/u/11427457?v=3","gravatar_id":"","url":"https://api.github.com/users/yuanweiwu","html_url":"https://github.com/yuanweiwu","followers_url":"https://api.github.com/users/yuanweiwu/followers","following_url":"https://api.github.com/users/yuanweiwu/following{/other_user}","gists_url":"https://api.github.com/users/yuanweiwu/gists{/gist_id}","starred_url":"https://api.github.com/users/yuanweiwu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yuanweiwu/subscriptions","organizations_url":"https://api.github.com/users/yuanweiwu/orgs","repos_url":"https://api.github.com/users/yuanweiwu/repos","events_url":"https://api.github.com/users/yuanweiwu/events{/privacy}","received_events_url":"https://api.github.com/users/yuanweiwu/received_events","type":"User","site_admin":false},"created_at":"2016-03-25T12:24:12Z","updated_at":"2016-03-25T12:24:12Z","body":"Some thoughts of mine after reviewing the code. :relaxed:\r\n\r\n1. A more uniform coding style\r\n\r\n `ch = ch + ''` --> `ch += ''`, as `pad += ch`, `len >>= 1` and `ch += ch`.\r\n\r\n2. The difference between `val + ''` and `val.toString()`\r\n\r\n Reference: http://www.2ality.com/2012/03/converting-to-string.html"}}