{"id":2537,"date":"2024-11-10T17:08:20","date_gmt":"2024-11-10T09:08:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2537\/"},"modified":"2024-11-10T17:08:20","modified_gmt":"2024-11-10T09:08:20","slug":"%e6%96%b9%e6%b3%95%e9%93%be%ef%bc%9afiltermap%e6%95%88%e7%8e%87%e4%bd%8e%e4%b8%8b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2537\/","title":{"rendered":"\u65b9\u6cd5\u94fe\uff1afilter()map()\u6548\u7387\u4f4e\u4e0b\uff1f"},"content":{"rendered":"<h2> \u65b9\u6cd5\u94fe\u63a5 <\/h2>\n<p>\u60a8\u53ef\u80fd\u89c1\u8fc7\u4f7f\u7528 array.prototype.filter() \u548c array.prototype.map() \u5728 javascript \u4e2d\u7f16\u8f91\u548c\u5220\u9664 array \u6570\u636e\u4e2d\u7684\u503c\u7684\u4ee3\u7801<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>[1,2,3]\n.map((mapped) =&gt; mapped + 1)\n.filter((filtered) =&gt; filtered &gt; 1)\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2> \u95ee\u9898 <\/h2>\n<p>\u6211\u4e00\u76f4\u60f3\u77e5\u9053\u8fd9\u6837\u7684<strong>\u65b9\u6cd5\u94fe\u63a5<\/strong>\u662f\u5426\u4f1a\u5728\u6bcf\u6b21\u8fed\u4ee3\u65f6\u8fed\u4ee3\u6570\u7ec4\u4e2d\u7684\u503c\uff0c\u6216\u8005v8\u5f15\u64ce\u662f\u5426\u53ef\u80fd\u901a\u8fc7\u5728\u540e\u53f0\u6267\u884c\u6570\u636e\u805a\u5408\u6765\u4f18\u5316\u64cd\u4f5c\u3002 <\/p>\n<p>\u6240\u4ee5\u6211\u8fdb\u884c\u4e86\u4e00\u9879\u5c0f\u7814\u7a76\u3002<\/p>\n<h2> \u5982\u4f55\uff1f <\/h2>\n<p>\u4f7f\u7528 console.time \u548c console.timeend \u6d4b\u91cf 10 \u6b21\u6709\u548c\u6ca1\u6709\u65b9\u6cd5\u94fe\u7684\u5e73\u5747\u6267\u884c\u65f6\u95f4\u3002<br \/> \u6211\u4f7f\u7528 chrome \u6d4f\u89c8\u5668\u8fdb\u884c\u8c03\u67e5\u3002<\/p>\n<pre>console.time('filter execution time')\n\/\/ your code\nconsole.timeend('filter execution time')\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u7ed3\u679c\uff1a\u65b9\u6cd5\u94fe\u63a5\u4e0d\u4f1a\u805a\u5408\u64cd\u4f5c\u3002 <\/h3>\n<table>\n<thead>\n<tr>\n<th>&#8211;<\/th>\n<th>method chn(a)<\/th>\n<th>no method chain(b)<\/th>\n<th>aggregate logic(c)<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>iterate 10000000 values<\/td>\n<td>4656 ms<\/td>\n<td>4733 ms<\/td>\n<td>169 ms<\/td>\n<\/tr>\n<tr>\n<td>iterate 100000 values<\/td>\n<td>27 ms<\/td>\n<td>24 ms<\/td>\n<td>4 ms<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6b63\u5982\u60a8\u6240\u770b\u5230\u7684\uff0c\u6709\/\u6ca1\u6709\u65b9\u6cd5\u94fe\u7684\u7ed3\u679c\u5e76\u6ca1\u6709\u663e\u793a\u51fa\u592a\u5927\u7684\u5dee\u5f02\uff0c\u800c\u805a\u5408\u903b\u8f91\u5219\u83b7\u5f97\u4e86\u66f4\u5feb\u7684\u7ed3\u679c\u3002 <\/p>\n<p><strong>\u4ee3\u7801-a\u3002\u5c06\u65b9\u6cd5\u94fe\u4e0emap()\u548cfilter()\u4e00\u8d77\u4f7f\u7528<\/strong><\/p>\n<pre>console.time('filter execution time')\nconst result = new array()\n.fill(1).map((e) =&gt; e + 1)\n.filter((e) =&gt; e !== 1)\nconsole.timeend('filter execution time')\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4ee3\u7801-b\u3002\u5206\u79bbmap()\u548cfilter()\uff08\u65e0\u65b9\u6cd5\u94fe\u63a5\uff09<\/strong><\/p>\n<pre>console.time('filter execution time')\nconst mapresult = new array().fill(1).map((e) =&gt; e + 1)\nconst result = mapresult.filter((e) =&gt; e !== 1)\nconsole.timeend('filter execution time')\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4ee3\u7801-c\u3002\u805a\u5408\u903b\u8f91<\/strong><\/p>\n<pre>console.time('Filter Execution Time')\nconst result = []\nnew Array().fill(1).forEach((e) =&gt;  {\n    if (e !== 1) result.push(e)\n})\nconsole.timeEnd('Filter Execution Time')\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u4f46\u662f\u65b9\u6cd5\u94fe\u5f88\u65b9\u4fbf\u4e0d\u662f\u5417\uff1f <\/h3>\n<p>\u5bf9\u4e8e\u90a3\u4e9b\u8fd9\u4e48\u8ba4\u4e3a\u7684\u4eba\uff0c\u6211\u5728 code pattern a\u3001b \u548c c \u4e0b\u4ece 100 \u5230 100,000 \u6b21\u8fed\u4ee3\u65f6\u5236\u4f5c\u4e86\u56fe\u8868\uff0c\u901a\u8fc7\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\u8ba1\u7b97 10 \u6b21\u7684\u5e73\u5747\u65f6\u95f4\uff0c\u4e0e\u4e0a\u9762\u7c7b\u4f3c\u3002 <\/p>\n<h3> \u53d1\u73b0 <\/h3>\n<ol>\n<li>\n<p>\u8fed\u4ee3\u6b21\u6570\u4e0d\u5927\uff08\u4f8b\u5982 100 &#8211; 1,000 \u6b21\uff09\u65f6\uff0c\u6ca1\u6709\u592a\u5927\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f53\u8fed\u4ee3\u6b21\u6570\u5448\u6307\u6570\u589e\u957f\uff08\u4f8b\u5982 1,000,000\uff09\u65f6\uff0c\u8fd9\u5c06\u5f88\u91cd\u8981\u3002<\/p>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173078694371552.jpg\" class=\"aligncenter\" title=\"\u65b9\u6cd5\u94fe\uff1afilter()map()\u6548\u7387\u4f4e\u4e0b\uff1f\u63d2\u56fe\" alt=\"\u65b9\u6cd5\u94fe\uff1afilter()map()\u6548\u7387\u4f4e\u4e0b\uff1f\u63d2\u56fe\" \/><\/p>\n<h2> \u7ed3\u8bba <\/h2>\n<p><strong>\u65b9\u6cd5\u94fe<\/strong>\u4e0d\u4f1a\u805a\u5408\u64cd\u4f5c\uff0c\u4f46\u7ed3\u679c\u7684\u5dee\u5f02\u968f\u7740\u8fed\u4ee3\u6b21\u6570\u5448\u6307\u6570\u7ea7\u589e\u957f\u800c\u6269\u5927\u3002 <\/p>\n<p>\u56e0\u6b64\u6211\u8ba4\u4e3a\u53ef\u4ee5\u516c\u5e73\u5730\u8bf4\uff0c\u5f53\u8fed\u4ee3\u6b21\u6570\u8f83\u5c11\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u65b9\u6cd5\u94fe\u6765\u83b7\u5f97\u5176\u4fbf\u6377\u6027\u7684\u4f18\u52bf\uff0c\u800c\u53e6\u4e00\u65b9\u9762\uff0c\u5982\u679c\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u5219\u5e94\u8be5\u4f7f\u7528\u65b9\u6cd5\u94fe\u4ed4\u7ec6\u8003\u8651\u7b97\u6cd5\uff0c\u4e0d\u8981\u8003\u8651\u65b9\u6cd5\u94fe\u3002 <\/p>\n<p>\u611f\u8c22\u60a8\u7684\u9605\u8bfb\uff01 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u65b9\u6cd5\u94fe\uff1afilter()map()\u6548\u7387\u4f4e\u4e0b\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u65b9\u6cd5\u94fe\u63a5 \u60a8\u53ef\u80fd\u89c1\u8fc7\u4f7f\u7528 array.prototype.filter() \u548c array.prototype.map() \u5728 javascript \u4e2d\u7f16\u8f91\u548c\u5220\u9664 array \u6570\u636e\u4e2d\u7684\u503c\u7684\u4ee3\u7801 \u4f8b\u5982\uff1a [1,2,3] .map((mapped) =&gt; mapped + 1) .filter((filtered) =&gt; filtered &gt; 1) \u767b\u5f55\u540e\u590d\u5236 \u95ee\u9898 \u6211\u4e00\u76f4\u60f3\u77e5\u9053\u8fd9\u6837\u7684\u65b9\u6cd5\u94fe\u63a5\u662f\u5426\u4f1a\u5728\u6bcf\u6b21\u8fed\u4ee3\u65f6\u8fed\u4ee3\u6570\u7ec4\u4e2d\u7684\u503c\uff0c\u6216\u8005v8\u5f15\u64ce\u662f\u5426\u53ef\u80fd\u901a\u8fc7\u5728\u540e\u53f0\u6267\u884c\u6570\u636e\u805a\u5408\u6765\u4f18\u5316\u64cd\u4f5c\u3002 \u6240\u4ee5\u6211\u8fdb\u884c\u4e86\u4e00\u9879\u5c0f\u7814\u7a76\u3002 \u5982\u4f55\uff1f \u4f7f\u7528 console.time \u548c console.timeend \u6d4b\u91cf 10 \u6b21\u6709\u548c\u6ca1\u6709\u65b9\u6cd5\u94fe\u7684\u5e73\u5747\u6267\u884c\u65f6\u95f4\u3002 \u6211\u4f7f\u7528 chrome \u6d4f\u89c8\u5668\u8fdb\u884c\u8c03\u67e5\u3002 console.time(&#8216;filter execution time&#8217;) \/\/ your code console.timeend(&#8216;filter execution time&#8217;) \u767b\u5f55\u540e\u590d\u5236 \u7ed3\u679c\uff1a\u65b9\u6cd5\u94fe\u63a5\u4e0d\u4f1a\u805a\u5408\u64cd\u4f5c\u3002 &#8211; method chn(a) no method chain(b) aggregate [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-2537","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=2537"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2537\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}