{"id":1189,"date":"2013-03-04T02:11:25","date_gmt":"2013-03-04T08:11:25","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=1189"},"modified":"2013-03-04T02:12:31","modified_gmt":"2013-03-04T08:12:31","slug":"re-implementation-of-javascripts-array-prototype-concat","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2013\/03\/re-implementation-of-javascripts-array-prototype-concat\/","title":{"rendered":"Re-implementation of Javascript&#8217;s Array.prototype.concat()"},"content":{"rendered":"<p>Just for fun I decided to see if I could write a faster version of `Array.prototype.concat()`.<\/p>\n<p><code><\/p>\n<pre lang=\"javascript\">\r\nvar concat = function (arr) {\r\n\tvar args = arguments,\r\n\tlen = args.length,\r\n\tArrayTypeOf = \"[object Array]\",\r\n\ttoString = Object.prototype.toString,\r\n\tpush = Array.prototype.push;\r\n\t\r\n\tfor (var i = 1; i < len; i++) {\r\n\t\tif (toString.call(args[i]) === ArrayTypeOf) {\r\n\t\t\tpush.apply(arr, args[i]);\r\n\t\t} else {\r\n\t\t\tarr.push(args[i]);\r\n\t\t}\r\n\t}\r\n\treturn arr;\r\n};\r\n<\/pre>\n<p><\/code><\/p>\n<p>Benchmark Script.<br \/>\n<code><\/p>\n<pre lang=\"javascript\">\r\nconsole.time(\"native concat()\");\r\nfor(var i = 0, len = 2020; i < len; i++) console.log( [].concat( new Array(1000).join(\"ax\").split(\"\") ).length );\r\nconsole.timeEnd(\"native concat()\");\r\n\r\nconsole.time(\"custom concat()\");\r\nfor(var i = 0, len = 2020; i < len; i++) console.log( concat( [], new Array(1000).join(\"ax\").split(\"\") ).length );\r\nconsole.timeEnd(\"custom concat()\");\r\n<\/pre>\n<p><\/code><\/p>\n<p>Benchmark results:<\/p>\n<p>native concat(): 386.000ms<br \/>\ncustom concat(): 400.000ms<\/p>\n<p>Well, I got quite close but didn't surpass the raw power of native code.<br \/>\nHere are some other alternatives to concat but with limited functionality.<br \/>\n<a href=\"http:\/\/jsperf.com\/concat-vs-push-apply\/19\">http:\/\/jsperf.com\/concat-vs-push-apply\/19<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just for fun I decided to see if I could write a faster version of `Array.prototype.concat()`. var concat = function (arr) { var args = arguments, len = args.length, ArrayTypeOf = &#8220;[object Array]&#8221;, toString = Object.prototype.toString, push = Array.prototype.push; for (var i = 1; i < len; i++) { if (toString.call(args[i]) === ArrayTypeOf) { push.apply(arr, &hellip; <a href=\"https:\/\/bateru.com\/news\/2013\/03\/re-implementation-of-javascripts-array-prototype-concat\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Re-implementation of Javascript&#8217;s Array.prototype.concat()<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[152,164],"class_list":["post-1189","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","tag-concat","tag-javascript"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=1189"}],"version-history":[{"count":4,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1189\/revisions"}],"predecessor-version":[{"id":1194,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1189\/revisions\/1194"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=1189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=1189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=1189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}