{"action":"opened","issue":{"url":"https://api.github.com/repos/josdejong/mathjs/issues/340","labels_url":"https://api.github.com/repos/josdejong/mathjs/issues/340/labels{/name}","comments_url":"https://api.github.com/repos/josdejong/mathjs/issues/340/comments","events_url":"https://api.github.com/repos/josdejong/mathjs/issues/340/events","html_url":"https://github.com/josdejong/mathjs/issues/340","id":70942231,"number":340,"title":"math.zeros cannot be optimized because of the way arguments is used","user":{"login":"mauritslamers","id":14616,"avatar_url":"https://avatars.githubusercontent.com/u/14616?v=3","gravatar_id":"","url":"https://api.github.com/users/mauritslamers","html_url":"https://github.com/mauritslamers","followers_url":"https://api.github.com/users/mauritslamers/followers","following_url":"https://api.github.com/users/mauritslamers/following{/other_user}","gists_url":"https://api.github.com/users/mauritslamers/gists{/gist_id}","starred_url":"https://api.github.com/users/mauritslamers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mauritslamers/subscriptions","organizations_url":"https://api.github.com/users/mauritslamers/orgs","repos_url":"https://api.github.com/users/mauritslamers/repos","events_url":"https://api.github.com/users/mauritslamers/events{/privacy}","received_events_url":"https://api.github.com/users/mauritslamers/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-04-25T17:37:00Z","updated_at":"2015-04-25T17:37:00Z","closed_at":null,"body":"While profiling my code which uses mathjs, I noticed that the use of mathjs.zeros could not be optimized because of the way arguments was used.\r\n\r\nSpecific spot:\r\nhttps://github.com/josdejong/mathjs/blob/master/lib/function/matrix/zeros.js#L49\r\n\r\nBecause arguments is used non-locally (in this case given as argument to a function which is not Function.apply), it needlessly slows down execution.\r\n\r\nIt is better to copy it locally, even if that means you add extra code. See also: \r\nhttps://github.com/petkaantonov/bluebird/wiki/Optimization-killers\r\n\r\nLooking at the code, adding copying code doesn't seem to be really necessary, as something like this should also work:\r\n\r\n```\r\n\t   math.zeros = function zeros (_m, _n, _format) {\r\n\t   \tvar numArgs = arguments.length;\r\n\t   \tvar m = _m,\r\n\t   \t\t\tn = _n,\r\n\t   \t\t\tformat = _format;\r\n\r\n\t   \tif (!format) {\r\n\t   \t\tif (m instanceof Matrix) {\r\n\t   \t\t\tformat = size.storage();\r\n\t   \t\t}\r\n\t   \t\telse if (!isArray(m) && config.matrix === 'matrix') {\r\n\t   \t\t\tformat = 'default';\r\n\t   \t\t}\r\n\t   \t}\r\n\r\n\t   \tvar args = [];\r\n\t   \tif (m) args.push(m)\r\n\t   \tif (n) args.push(n);\r\n\t   \r\n\t   // convert arguments from bignumber to numbers if needed\r\n\t   var asBigNumber = false;\r\n\t   // map arguments & validate\r\n\t   args = args.map(function (value) {\r\n\t     // check it is a big number\r\n\t     if (value instanceof BigNumber) {\r\n\t       // set flag\r\n\t       asBigNumber = true;\r\n\t       // convert it\r\n\t       value = value.toNumber();\r\n\t     }\r\n\t     // validate arguments\r\n\t     if (!isNumber(value) || !isInteger(value) || value < 0) {\r\n\t       throw new Error('Parameters in function eye must be positive integers');\r\n\t     }\r\n\t     return value;\r\n\t   });\r\n\r\n\t   \t// default value\r\n\t   \tvar defaultValue = asBigNumber ? new BigNumber(0) : 0;\r\n\r\n\t   \r\n\t   \t// check we need to return a matrix\r\n\t   \tif (format) {\r\n\t   \t  // create empty matrix\r\n\t   \t  var m = math.matrix(format);\r\n\t   \t  // check we need to resize matrix\r\n\t   \t  if (args.length > 0) {\r\n\t   \t    // resize it to correct size\r\n\t   \t    return m.resize(args, defaultValue);\r\n\t   \t  }\r\n\t   \t  return m;\r\n\t   \t}\r\n\t   \t// empty array\r\n\t   \tvar res = [];\r\n\t   \t// check we need to resize array\r\n\t   \tif (args.length > 0) {\r\n\t   \t  // resize array\r\n\t   \t  return array.resize(res, args, defaultValue);\r\n\t   \t}\r\n\t   \treturn res;\r\n\r\n\t   };\r\n```\r\n\r\nThis code example is taken from mathjs 1.5.1.\r\n\r\nBTW: it is kind of strange to see the error telling that this function is eye...\r\n\r\n"}}