{"id":502,"date":"2025-12-05T14:51:40","date_gmt":"2025-12-05T06:51:40","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=502"},"modified":"2025-12-05T14:51:40","modified_gmt":"2025-12-05T06:51:40","slug":"javascript%e4%b8%adsliced-string%e5%af%bc%e8%87%b4%e5%86%85%e5%ad%98%e6%97%a0%e6%b3%95%e9%87%8a%e6%94%be%e7%9a%84%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/javascript%e4%b8%adsliced-string%e5%af%bc%e8%87%b4%e5%86%85%e5%ad%98%e6%97%a0%e6%b3%95%e9%87%8a%e6%94%be%e7%9a%84%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95\/","title":{"rendered":"JavaScript\u4e2dsliced\u00a0string\u5bfc\u81f4\u5185\u5b58\u65e0\u6cd5\u91ca\u653e\u7684\u89e3\u51b3\u65b9\u6cd5"},"content":{"rendered":"\n<p>\u5728 JavaScript \u4e2d\uff0c\u5b57\u7b26\u4e32\u5207\u7247\uff08<code>slice<\/code>\u3001<code>substring<\/code>\u3001<code>substr<\/code> \u7b49\uff09\u901a\u5e38\u4e0d\u4f1a\u4ea7\u751f\u5185\u5b58\u6cc4\u6f0f\uff0c\u4f46\u5728 <strong>\u5904\u7406\u8d85\u5927\u5b57\u7b26\u4e32\u6216\u957f\u671f\u6301\u6709\u5b50\u4e32\u5f15\u7528<\/strong> \u65f6\uff0c\u53ef\u80fd\u51fa\u73b0 <strong>\u5185\u5b58\u65e0\u6cd5\u91ca\u653e<\/strong> \u7684\u60c5\u51b5\u3002\u8fd9\u901a\u5e38\u4e0e <strong>\u5e95\u5c42\u5b57\u7b26\u4e32\u5171\u4eab\u673a\u5236<\/strong>\uff08string interning\uff09\u6709\u5173\u3002\u4e0b\u9762\u6211\u5e2e\u4f60\u8be6\u7ec6\u5206\u6790\u539f\u56e0\uff0c\u5e76\u7ed9\u51fa\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">1\ufe0f\u20e3 \u95ee\u9898\u539f\u56e0<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>JavaScript \u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>slice<\/code> \u8fd4\u56de\u4e00\u4e2a <strong>\u65b0\u5b57\u7b26\u4e32<\/strong><\/li>\n\n\n\n<li>\u4f46\u5728\u67d0\u4e9b JS \u5f15\u64ce\u4e2d\uff0c\u4e3a\u4e86\u4f18\u5316\u6027\u80fd\uff0c\u5b50\u5b57\u7b26\u4e32\u53ef\u80fd <strong>\u5171\u4eab\u539f\u59cb\u5b57\u7b26\u4e32\u7684\u5185\u5b58<\/strong><\/li>\n\n\n\n<li>\u5982\u679c\u539f\u59cb\u5b57\u7b26\u4e32\u975e\u5e38\u5927\uff0c\u800c\u4f60\u53ea\u4fdd\u7559\u5c0f\u7247\u6bb5\uff0c\u6574\u4e2a\u5927\u5b57\u7b26\u4e32\u53ef\u80fd\u65e0\u6cd5\u88ab GC \u56de\u6536<\/li>\n<\/ul>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst largeStr = new Array(10_000_000).join(&#039;a&#039;) + &#039;END&#039;;\nconst smallStr = largeStr.slice(-3); \/\/ &#039;END&#039;\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c JS \u5f15\u64ce\u5171\u4eab\u5185\u5b58\uff0c<code>largeStr<\/code> \u5373\u4f7f\u4e0d\u518d\u4f7f\u7528\uff0c\u53ef\u80fd\u4ecd\u88ab <code>smallStr<\/code> \u6301\u6709\u5f15\u7528\uff0c\u65e0\u6cd5\u91ca\u653e<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">2\ufe0f\u20e3 \u89e3\u51b3\u65b9\u6cd5<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u65b9\u6cd5\u4e00\uff1a\u663e\u5f0f\u590d\u5236\u5b50\u5b57\u7b26\u4e32<\/h2>\n\n\n\n<p>\u901a\u8fc7 <code>String.prototype.slice<\/code> \u6216 <code>'' + str<\/code> \u7b49\u65b9\u5f0f\u751f\u6210 <strong>\u771f\u6b63\u72ec\u7acb\u7684\u65b0\u5b57\u7b26\u4e32<\/strong>\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u65b9\u6cd5 1\uff1a\u4f7f\u7528 concat \u751f\u6210\u65b0\u5b57\u7b26\u4e32\nconst smallStr = (&#039;&#039; + largeStr.slice(-3));\n\n\/\/ \u65b9\u6cd5 2\uff1aArray.join\nconst smallStr2 = largeStr.slice(-3).split(&#039;&#039;).join(&#039;&#039;);\n\n<\/pre><\/div>\n\n\n<p>\u2705 \u539f\u7406\uff1a\u751f\u6210\u7684\u65b0\u5b57\u7b26\u4e32\u4e0d\u518d\u5f15\u7528\u539f\u59cb\u5927\u5b57\u7b26\u4e32\uff0c\u5185\u5b58\u53ef\u4ee5\u88ab\u56de\u6536<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528 <code>TextEncoder<\/code> \/ <code>Uint8Array<\/code> \u5904\u7406\u5927\u6587\u672c<\/h2>\n\n\n\n<p>\u5982\u679c\u5904\u7406\u5927\u91cf\u6587\u672c\u6570\u636e\uff0c\u5c24\u5176\u662f\u4e8c\u8fdb\u5236\u6216\u6587\u4ef6\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528 <strong>ArrayBuffer \/ Uint8Array<\/strong>\uff0c\u907f\u514d\u5b57\u7b26\u4e32\u5171\u4eab\u5bfc\u81f4\u7684\u5185\u5b58\u5360\u7528\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst encoder = new TextEncoder();\nconst largeStr = &#039;a&#039;.repeat(10_000_000);\nconst buffer = encoder.encode(largeStr); \/\/ Uint8Array\nconst smallSlice = buffer.subarray(buffer.length - 3);\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><code>subarray<\/code> \u8fd4\u56de\u7684\u53ea\u5305\u542b\u5fc5\u8981\u90e8\u5206\u7684\u5185\u5b58<\/li>\n\n\n\n<li>\u539f\u59cb\u5927\u5b57\u7b26\u4e32\u53ef\u4ee5\u88ab\u56de\u6536<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u65b9\u6cd5\u4e09\uff1a\u907f\u514d\u957f\u671f\u6301\u6709\u5927\u5b57\u7b26\u4e32\u5f15\u7528<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlet largeStr = &#039;...\u8d85\u5927\u5b57\u7b26\u4e32...&#039;;\nlet smallStr = largeStr.slice(-3);\n\n\/\/ \u53ca\u65f6\u91ca\u653e\nlargeStr = null; \/\/ GC \u53ef\u56de\u6536\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>JS \u5783\u573e\u56de\u6536\u5668\u4f1a\u56de\u6536\u672a\u88ab\u5f15\u7528\u7684\u5bf9\u8c61<\/li>\n\n\n\n<li><strong>\u5343\u4e07\u4e0d\u8981\u5728\u5168\u5c40\u4f5c\u7528\u57df\u957f\u671f\u6301\u6709\u5927\u5b57\u7b26\u4e32<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u65b9\u6cd5\u56db\uff1a\u5206\u5757\u5904\u7406\u5927\u5b57\u7b26\u4e32<\/h2>\n\n\n\n<p>\u5904\u7406\u8d85\u5927\u65e5\u5fd7\u6587\u4ef6\u3001\u6587\u672c\u6570\u636e\u65f6\uff0c\u4e0d\u8981\u4e00\u6b21\u6027\u8bfb\u53d6\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst CHUNK_SIZE = 1024 * 1024;\nfor (let i = 0; i &amp;lt; largeStr.length; i += CHUNK_SIZE) {\n  const chunk = largeStr.slice(i, i + CHUNK_SIZE);\n  processChunk(chunk); \/\/ \u5904\u7406\u5b8c\u5373\u53ef\u4e22\u5f03\n}\n\n<\/pre><\/div>\n\n\n<p>\u2705 \u4f18\u52bf\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u907f\u514d slice \u5bfc\u81f4\u5927\u5b57\u7b26\u4e32\u6301\u7eed\u5360\u7528\u5185\u5b58<\/li>\n\n\n\n<li>\u6bcf\u6b21\u53ea\u4fdd\u7559\u5fc5\u8981\u90e8\u5206<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">3\ufe0f\u20e3 \u603b\u7ed3<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u573a\u666f<\/th><th>\u95ee\u9898<\/th><th>\u89e3\u51b3\u65b9\u6cd5<\/th><\/tr><\/thead><tbody><tr><td>\u8d85\u5927\u5b57\u7b26\u4e32 slice \u5b50\u4e32<\/td><td>\u5b50\u4e32\u53ef\u80fd\u6301\u6709\u6574\u4e2a\u539f\u5b57\u7b26\u4e32\u5f15\u7528\uff0cGC \u65e0\u6cd5\u56de\u6536<\/td><td>\u663e\u5f0f\u751f\u6210\u65b0\u5b57\u7b26\u4e32\uff1a<code>'' + str.slice()<\/code><\/td><\/tr><tr><td>\u6301\u4e45\u5b58\u50a8\u5b50\u4e32<\/td><td>\u5185\u5b58\u5360\u7528<\/td><td>\u53ca\u65f6\u91ca\u653e\u5927\u5b57\u7b26\u4e32\u5f15\u7528 <code>largeStr = null<\/code><\/td><\/tr><tr><td>\u5904\u7406\u6587\u4ef6\/\u4e8c\u8fdb\u5236\u6587\u672c<\/td><td>\u5b57\u7b26\u4e32\u5171\u4eab\u5bfc\u81f4\u5927\u5185\u5b58<\/td><td>\u4f7f\u7528 <code>Uint8Array<\/code> \u6216 <code>TextEncoder<\/code><\/td><\/tr><tr><td>\u5927\u6587\u672c\u5206\u5757\u5904\u7406<\/td><td>\u4e00\u6b21\u6027 slice \u5360\u7528\u5927\u91cf\u5185\u5b58<\/td><td>\u6309\u5757 slice \u5904\u7406<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">4\ufe0f\u20e3 \u63a8\u8350\u5b9e\u8df5<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction safeSlice(str: string, start: number, end?: number): string {\n  return (&#039;&#039; + str.slice(start, end)); \/\/ \u72ec\u7acb\u5b57\u7b26\u4e32\uff0c\u907f\u514d\u6301\u6709\u539f\u59cb\u5927\u5b57\u7b26\u4e32\n}\n\n\/\/ \u4f7f\u7528\nlet largeStr = &#039;a&#039;.repeat(10_000_000);\nlet smallStr = safeSlice(largeStr, -3);\nlargeStr = null; \/\/ \u53ef\u4ee5\u56de\u6536\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>\u2705 \u9002\u7528\u4e8e\u8d85\u5927\u5b57\u7b26\u4e32\u64cd\u4f5c<\/li>\n\n\n\n<li>\u2705 \u4fdd\u8bc1\u5c0f\u5b57\u7b26\u4e32\u4e0d\u5f15\u7528\u5927\u5b57\u7b26\u4e32<\/li>\n\n\n\n<li>\u2705 \u907f\u514d\u5185\u5b58\u6cc4\u6f0f<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 JavaScript \u4e2d\uff0c\u5b57\u7b26\u4e32\u5207\u7247\uff08slice\u3001substring\u3001sub&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/javascript%e4%b8%adsliced-string%e5%af%bc%e8%87%b4%e5%86%85%e5%ad%98%e6%97%a0%e6%b3%95%e9%87%8a%e6%94%be%e7%9a%84%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-502","post","type-post","status-publish","format-standard","hentry","category-javascript"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=502"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/502\/revisions"}],"predecessor-version":[{"id":503,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/502\/revisions\/503"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}