{"id":1888,"date":"2020-07-18T15:43:20","date_gmt":"2020-07-18T07:43:20","guid":{"rendered":"http:\/\/www.ouvps.com\/?p=1888"},"modified":"2020-07-18T16:01:28","modified_gmt":"2020-07-18T08:01:28","slug":"%e8%bd%acjavascript-%e9%ab%98%e6%80%a7%e8%83%bd%e6%95%b0%e7%bb%84%e5%8e%bb%e9%87%8d","status":"publish","type":"post","link":"https:\/\/eepu.top\/?p=1888","title":{"rendered":"[\u8f6c]JavaScript \u9ad8\u6027\u80fd\u6570\u7ec4\u53bb\u91cd"},"content":{"rendered":"<p>\u4e2d\u5348\u548c\u540c\u4e8b\u5403\u996d\uff0c\u5e2d\u95f4\u8ba8\u8bba\u5230\u6570\u7ec4\u53bb\u91cd\u8fd9\u4e00\u95ee\u9898<\/p>\n<p>\u6211\u7acb\u523b\u5c31\u5206\u4eab\u4e86\u6211\u5e38\u7528\u7684\u4e00\u4e2a\u53bb\u91cd\u65b9\u6cd5\uff0c\u968f\u5373\u88ab\u8001\u5927\u6307\u51fa\u8fd9\u4e2a\u65b9\u6cd5\u6548\u7387\u4e0d\u9ad8<\/p>\n<p>\u56de\u5bb6\u540e\u6211\u81ea\u5df1\u6d4b\u8bd5\u4e86\u4e00\u4e0b\uff0c\u53d1\u73b0\u90a3\u4e2a\u65b9\u6cd5\u786e\u5b9e\u5f88\u6162<\/p>\n<p>\u4e8e\u662f\u5c31\u6709\u4e86\u8fd9\u4e00\u6b21\u7684\u9ad8\u6027\u80fd\u6570\u7ec4\u53bb\u91cd\u7814\u7a76<\/p>\n<p>\u4e00\u3001\u6d4b\u8bd5\u6a21\u7248<\/p>\n<p>\u6570\u7ec4\u53bb\u91cd\u662f\u4e00\u4e2a\u8001\u751f\u5e38\u8c08\u7684\u95ee\u9898\uff0c\u7f51\u4e0a\u6d41\u4f20\u7740\u6709\u5404\u79cd\u5404\u6837\u7684\u89e3\u6cd5<\/p>\n<p>\u4e3a\u4e86\u6d4b\u8bd5\u8fd9\u4e9b\u89e3\u6cd5\u7684\u6027\u80fd\uff0c\u6211\u5199\u4e86\u4e00\u4e2a\u6d4b\u8bd5\u6a21\u7248\uff0c\u7528\u6765\u8ba1\u7b97\u6570\u7ec4\u53bb\u91cd\u7684\u8017\u65f6<\/p>\n<pre>\/\/ distinct.js\n\nlet arr1 = Array.from(new Array(100000), (x, index)=&gt;{\n    return index\n})\n\nlet arr2 = Array.from(new Array(50000), (x, index)=&gt;{\n    return index+index\n})\n\nlet start = new Date().getTime()\nconsole.log('\u5f00\u59cb\u6570\u7ec4\u53bb\u91cd')\n\nfunction distinct(a, b) {\n    \/\/ \u6570\u7ec4\u53bb\u91cd\n}\n\nconsole.log('\u53bb\u91cd\u540e\u7684\u957f\u5ea6', distinct(arr1, arr2).length)\n\nlet end = new Date().getTime()\nconsole.log('\u8017\u65f6', end - start)\n<\/pre>\n<p>\u8fd9\u91cc\u5206\u522b\u521b\u5efa\u4e86\u4e24\u4e2a\u957f\u5ea6\u4e3a 10W \u548c 5W \u7684\u6570\u7ec4<\/p>\n<p>\u7136\u540e\u901a\u8fc7 distinct() \u65b9\u6cd5\u5408\u5e76\u4e24\u4e2a\u6570\u7ec4\uff0c\u5e76\u53bb\u6389\u5176\u4e2d\u7684\u91cd\u590d\u9879<\/p>\n<p>\u6570\u636e\u91cf\u4e0d\u5927\u4e5f\u4e0d\u5c0f\uff0c\u4f46\u5df2\u7ecf\u80fd\u8bf4\u660e\u4e00\u4e9b\u95ee\u9898\u4e86<\/p>\n<p>\u4e8c\u3001Array.filter() + indexOf<\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u7684\u601d\u8def\u662f\uff0c\u5c06\u4e24\u4e2a\u6570\u7ec4\u62fc\u63a5\u4e3a\u4e00\u4e2a\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528 ES6 \u4e2d\u7684 Array.filter() \u904d\u5386\u6570\u7ec4\uff0c\u5e76\u7ed3\u5408 indexOf \u6765\u6392\u9664\u91cd\u590d\u9879<\/p>\n<pre>function distinct(a, b) {\n    let arr = a.concat(b);\n    return arr.filter((item, index)=&gt; {\n        return arr.indexOf(item) === index\n    })\n}\n<\/pre>\n<p>\u8fd9\u5c31\u662f\u6211\u88ab\u5410\u69fd\u7684\u90a3\u4e2a\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\uff0c\u770b\u8d77\u6765\u975e\u5e38\u7b80\u6d01\uff0c\u4f46\u5b9e\u9645\u6027\u80fd\u3002\u3002\u3002<\/p>\n<p><a href=\"http:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/0.png\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-1889\" src=\"http:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/0-300x90.png\" alt=\"\" width=\"300\" height=\"90\" srcset=\"https:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/0-300x90.png 300w, https:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/0.png 448w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u662f\u7684\uff0c\u73b0\u5b9e\u5c31\u662f\u8fd9\u4e48\u6b8b\u9177\uff0c\u5904\u7406\u4e00\u4e2a\u957f\u5ea6\u4e3a 15W \u7684\u6570\u7ec4\u90fd\u9700\u8981 8427ms<\/p>\n<p>\u4e09\u3001\u53cc\u91cd for \u5faa\u73af<\/p>\n<p>\u6700\u5bb9\u6613\u7406\u89e3\u7684\u65b9\u6cd5\uff0c\u5916\u5c42\u5faa\u73af\u904d\u5386\u5143\u7d20\uff0c\u5185\u5c42\u5faa\u73af\u68c0\u67e5\u662f\u5426\u91cd\u590d<\/p>\n<p>\u5f53\u6709\u91cd\u590d\u503c\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4f7f\u7528 push()\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 splice()<\/p>\n<pre>function distinct(a, b) {\n    let arr = a.concat(b);\n    for (let i=0, len=arr.length; i&lt;len; i++) {\n        for (let j=i+1; j&lt;len; j++) {\n            if (arr[i] == arr[j]) {\n                arr.splice(j, 1);\n                \/\/ splice \u4f1a\u6539\u53d8\u6570\u7ec4\u957f\u5ea6\uff0c\u6240\u4ee5\u8981\u5c06\u6570\u7ec4\u957f\u5ea6 len \u548c\u4e0b\u6807 j \u51cf\u4e00\n                len--;\n                j--;\n            }\n        }\n    }\n    return arr\n}\n<\/pre>\n<p>\u4f46\u8fd9\u79cd\u65b9\u6cd5\u5360\u7528\u7684\u5185\u5b58\u8f83\u9ad8\uff0c\u6548\u7387\u4e5f\u662f\u6700\u4f4e\u7684 <a=\"\" href=\"http:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/1.png\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-1890\" src=\"http:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/1-300x98.png\" alt=\"\" width=\"300\" height=\"98\" srcset=\"https:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/1-300x98.png 300w, https:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/1.png 436w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a=\"\"><\/p>\n<p>\u56db\u3001for&#8230;of + includes()<\/p>\n<p>\u53cc\u91cdfor\u5faa\u73af\u7684\u5347\u7ea7\u7248\uff0c\u5916\u5c42\u7528 for&#8230;of \u8bed\u53e5\u66ff\u6362 for \u5faa\u73af\uff0c\u628a\u5185\u5c42\u5faa\u73af\u6539\u4e3a includes()<\/p>\n<p>\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u6570\u7ec4\uff0c\u5f53 includes() \u8fd4\u56de false \u7684\u65f6\u5019\uff0c\u5c31\u5c06\u8be5\u5143\u7d20 push \u5230\u7a7a\u6570\u7ec4\u4e2d<\/p>\n<p>\u7c7b\u4f3c\u7684\uff0c\u8fd8\u53ef\u4ee5\u7528 indexOf() \u6765\u66ff\u4ee3 includes()<\/p>\n<pre>function distinct(a, b) {\n    let arr = a.concat(b)\n    let result = []\n    for (let i of arr) {\n        !result.includes(i) &amp;&amp; result.push(i)\n    }\n    return result\n}\n<\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u548c filter + indexOf \u633a\u7c7b\u4f3c<\/p>\n<p>\u53ea\u662f\u628a filter() \u7684\u5185\u90e8\u903b\u8f91\u7528 for \u5faa\u73af\u5b9e\u73b0\u51fa\u6765\uff0c\u518d\u628a indexOf \u6362\u4e3a includes<\/p>\n<p>\u6240\u4ee5\u65f6\u957f\u4e0a\u4e5f\u6bd4\u8f83\u63a5\u8fd1<\/p>\n<p><a href=\"http:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/2.png\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-1891\" src=\"http:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/2-300x93.png\" alt=\"\" width=\"300\" height=\"93\" srcset=\"https:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/2-300x93.png 300w, https:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/2.png 434w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u4e94\u3001Array.sort()<\/p>\n<p>\u9996\u5148\u4f7f\u7528 sort() \u5c06\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f<\/p>\n<p>\u7136\u540e\u6bd4\u8f83\u76f8\u90bb\u5143\u7d20\u662f\u5426\u76f8\u7b49\uff0c\u4ece\u800c\u6392\u9664\u91cd\u590d\u9879<\/p>\n<pre>function distinct(a, b) {\n    let arr = a.concat(b)\n    arr = arr.sort()\n    let result = [arr[0]]\n\n    for (let i=1, len=arr.length; i&lt;len; i++) {\n        arr[i] !== arr[i-1] &amp;&amp; result.push(arr[i])\n    }\n    return result\n}\n<\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ea\u505a\u4e86\u4e00\u6b21\u6392\u5e8f\u548c\u4e00\u6b21\u5faa\u73af\uff0c\u6240\u4ee5\u6548\u7387\u4f1a\u6bd4\u4e0a\u9762\u7684\u65b9\u6cd5\u90fd\u8981\u9ad8<br \/>\n<a href=\"http:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/3.png\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-1892\" src=\"http:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/3-300x93.png\" alt=\"\" width=\"300\" height=\"93\" srcset=\"https:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/3-300x93.png 300w, https:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/3.png 430w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u516d\u3001new Set()<\/p>\n<p>ES6 \u65b0\u589e\u4e86 Set \u8fd9\u4e00\u6570\u636e\u7ed3\u6784\uff0c\u7c7b\u4f3c\u4e8e\u6570\u7ec4\uff0c\u4f46 Set \u7684\u6210\u5458\u5177\u6709\u552f\u4e00\u6027<\/p>\n<p>\u57fa\u4e8e\u8fd9\u4e00\u7279\u6027\uff0c\u5c31\u975e\u5e38\u9002\u5408\u7528\u6765\u505a\u6570\u7ec4\u53bb\u91cd\u4e86<\/p>\n<p>function distinct(a, b) {<br \/>\nreturn Array.from(new Set([&#8230;a, &#8230;b]))<br \/>\n}<br \/>\n\u90a3\u4f7f\u7528 Set \u53c8\u9700\u8981\u591a\u4e45\u65f6\u95f4\u6765\u5904\u7406 15W \u7684\u6570\u636e\u5462\uff1f<br \/>\n<a href=\"http:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/4.png\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-1893\" src=\"http:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/4-300x87.png\" alt=\"\" width=\"300\" height=\"87\" srcset=\"https:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/4-300x87.png 300w, https:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/4.png 448w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u55b5\u55b5\u55b5\uff1f\uff1f\uff1f 57ms \uff1f\uff1f\u6211\u6ca1\u773c\u82b1\u5427\uff1f\uff1f<\/p>\n<p>\u7136\u540e\u6211\u5728\u4e24\u4e2a\u6570\u7ec4\u957f\u5ea6\u540e\u9762\u5206\u522b\u52a0\u4e86\u4e00\u4e2a0\uff0c\u5728 150W \u7684\u6570\u636e\u91cf\u4e4b\u4e0b&#8230;<br \/>\n<a href=\"http:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/5.png\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-1894\" src=\"http:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/5-300x100.png\" alt=\"\" width=\"300\" height=\"100\" srcset=\"https:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/5-300x100.png 300w, https:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/5.png 424w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u5c45\u7136\u6709\u5982\u6b64\u9ad8\u6027\u80fd\u4e14\u7b80\u6d01\u7684\u6570\u7ec4\u53bb\u91cd\u529e\u6cd5\uff1f\uff01<\/p>\n<p>\u4e03\u3001for&#8230;of + Object<\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u6211\u53ea\u5728\u4e00\u4e9b\u6587\u7ae0\u91cc\u89c1\u8fc7\uff0c\u5b9e\u9645\u5de5\u4f5c\u4e2d\u5012\u6ca1\u600e\u4e48\u7528<\/p>\n<p>\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u5bf9\u8c61\uff0c\u7136\u540e\u7528 for \u5faa\u73af\u904d\u5386<\/p>\n<p>\u5229\u7528\u5bf9\u8c61\u7684\u5c5e\u6027\u4e0d\u4f1a\u91cd\u590d\u8fd9\u4e00\u7279\u6027\uff0c\u6821\u9a8c\u6570\u7ec4\u5143\u7d20\u662f\u5426\u91cd\u590d<\/p>\n<pre>function distinct(a, b) {\n    let arr = a.concat(b)\n    let result = []\n    let obj = {}\n\n    for (let i of arr) {\n        if (!obj[i]) {\n            result.push(i)\n            obj[i] = 1\n        }\n    }\n\n    return result\n}\n<\/pre>\n<p>\u5f53\u6211\u770b\u5230\u8fd9\u4e2a\u65b9\u6cd5\u7684\u5904\u7406\u65f6\u957f\uff0c\u6211\u53c8\u50bb\u773c\u4e86<\/p>\n<p><a href=\"http:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/6.png\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-1895\" src=\"http:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/6-300x90.png\" alt=\"\" width=\"300\" height=\"90\" srcset=\"https:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/6-300x90.png 300w, https:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/6.png 440w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>15W \u7684\u6570\u636e\u5c45\u7136\u53ea\u8981 16ms \uff1f\uff1f\uff1f \u6bd4 Set() \u8fd8\u5feb\uff1f\uff1f\uff1f<\/p>\n<p>\u7136\u540e\u6211\u53c8\u8bd5\u4e86\u8bd5 150W \u7684\u6570\u636e\u91cf&#8230;<\/p>\n<p><a href=\"http:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/7.png\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-1896\" src=\"http:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/7-300x90.png\" alt=\"\" width=\"300\" height=\"90\" srcset=\"https:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/7-300x90.png 300w, https:\/\/eepu.top\/wp-content\/uploads\/2020\/07\/7.png 438w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>emmmmmmm&#8230;. \u60f9\u4e0d\u8d77\u60f9\u4e0d\u8d77&#8230;<\/p>\n<p>\u8f6c\u81ea\uff1ahttps:\/\/www.cnblogs.com\/wisewrong\/p\/9642264.html<\/p>\n<pre><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e2d\u5348\u548c\u540c\u4e8b\u5403\u996d\uff0c\u5e2d\u95f4\u8ba8\u8bba\u5230\u6570\u7ec4\u53bb\u91cd\u8fd9\u4e00\u95ee\u9898 \u6211\u7acb\u523b\u5c31\u5206\u4eab\u4e86\u6211\u5e38\u7528\u7684\u4e00\u4e2a\u53bb\u91cd\u65b9\u6cd5\uff0c\u968f\u5373\u88ab\u8001\u5927\u6307\u51fa\u8fd9\u4e2a\u65b9\u6cd5\u6548\u7387\u4e0d\u9ad8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1889,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[73],"tags":[],"_links":{"self":[{"href":"https:\/\/eepu.top\/index.php?rest_route=\/wp\/v2\/posts\/1888"}],"collection":[{"href":"https:\/\/eepu.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eepu.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eepu.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eepu.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1888"}],"version-history":[{"count":7,"href":"https:\/\/eepu.top\/index.php?rest_route=\/wp\/v2\/posts\/1888\/revisions"}],"predecessor-version":[{"id":1903,"href":"https:\/\/eepu.top\/index.php?rest_route=\/wp\/v2\/posts\/1888\/revisions\/1903"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eepu.top\/index.php?rest_route=\/wp\/v2\/media\/1889"}],"wp:attachment":[{"href":"https:\/\/eepu.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eepu.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eepu.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}