{"id":1199,"date":"2024-11-23T18:25:41","date_gmt":"2024-11-23T23:25:41","guid":{"rendered":"https:\/\/www.oopus.info\/blog\/?p=1199"},"modified":"2024-11-24T00:47:06","modified_gmt":"2024-11-24T05:47:06","slug":"foreach-and-shadow-copy-in-javascript","status":"publish","type":"post","link":"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/","title":{"rendered":"Debugging Pitfalls: Async forEach and Slice Memory Sharing in JavaScript"},"content":{"rendered":"\n<p>From last Thursday until just now (Saturday), two issues confused me a lot:<\/p>\n\n\n\n<p><strong><code>forEach<\/code> is synchronous, not asynchronous<\/strong><\/p>\n\n\n\n<p>This is a fundamental concept, but problems can arise when you assume a block of code will execute synchronously while using <code>forEach<\/code>. For some reason, if that block of code becomes asynchronous, it could lead to unexpected issues.<\/p>\n\n\n\n<p><strong><code>slice<\/code> creates a shallow copy<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>shortConversation = &#91;\u2026systemPrompt, \u2026chatThread.current.slice(-(memoryLength + 2 - 1))];<\/code><\/code><\/pre>\n\n\n\n<p>The <code>slice<\/code> method causes <code>shortConversation<\/code> and <code>chatThread<\/code> to share part of the memory, which led to unexpected behaviors.<\/p>\n\n\n\n<p>The corrected code is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>shortConversation = &#91;...systemPrompt, ...structuredClone(chatThread.current.slice(-(memoryLength + 2 - 1)))];<\/code><\/pre>\n\n\n\n<p>Using <code>structuredClone<\/code> ensures that <code>shortConversation<\/code> and <code>chatThread<\/code> are completely independent.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>From last Thursday until just now (Saturday), two issues confused me a lot: forEach is synchronous, not asynchronous This is a fundamental concept, but problems can arise when you assume a block of code will execute synchronously while using forEach. For some reason, if that block of code becomes asynchronous, it could lead to unexpected [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1199","post","type-post","status-publish","format-standard","hentry","category-computer-science"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Debugging Pitfalls: Async forEach and Slice Memory Sharing in JavaScript - Oopus Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Debugging Pitfalls: Async forEach and Slice Memory Sharing in JavaScript - Oopus Blog\" \/>\n<meta property=\"og:description\" content=\"From last Thursday until just now (Saturday), two issues confused me a lot: forEach is synchronous, not asynchronous This is a fundamental concept, but problems can arise when you assume a block of code will execute synchronously while using forEach. For some reason, if that block of code becomes asynchronous, it could lead to unexpected [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Oopus Blog\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-23T23:25:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-24T05:47:06+00:00\" \/>\n<meta name=\"author\" content=\"oopus\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"oopus\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/\"},\"author\":{\"name\":\"oopus\",\"@id\":\"https:\/\/www.oopus.info\/blog\/#\/schema\/person\/0491ca0bc5ba1f23b62bb7be621f29c9\"},\"headline\":\"Debugging Pitfalls: Async forEach and Slice Memory Sharing in JavaScript\",\"datePublished\":\"2024-11-23T23:25:41+00:00\",\"dateModified\":\"2024-11-24T05:47:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/\"},\"wordCount\":96,\"articleSection\":[\"computer science\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/\",\"url\":\"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/\",\"name\":\"Debugging Pitfalls: Async forEach and Slice Memory Sharing in JavaScript - Oopus Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.oopus.info\/blog\/#website\"},\"datePublished\":\"2024-11-23T23:25:41+00:00\",\"dateModified\":\"2024-11-24T05:47:06+00:00\",\"author\":{\"@id\":\"https:\/\/www.oopus.info\/blog\/#\/schema\/person\/0491ca0bc5ba1f23b62bb7be621f29c9\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.oopus.info\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Debugging Pitfalls: Async forEach and Slice Memory Sharing in JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.oopus.info\/blog\/#website\",\"url\":\"https:\/\/www.oopus.info\/blog\/\",\"name\":\"Oopus Blog\",\"description\":\"Oopu&#039;s self-recording\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.oopus.info\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.oopus.info\/blog\/#\/schema\/person\/0491ca0bc5ba1f23b62bb7be621f29c9\",\"name\":\"oopus\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/e6169b542ea65ef728fae42c117aa13b4897842f8263161765d1384a2a6e6fd9?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e6169b542ea65ef728fae42c117aa13b4897842f8263161765d1384a2a6e6fd9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e6169b542ea65ef728fae42c117aa13b4897842f8263161765d1384a2a6e6fd9?s=96&d=mm&r=g\",\"caption\":\"oopus\"},\"sameAs\":[\"http:\/\/www.oopus.info\/blog\"],\"url\":\"https:\/\/www.oopus.info\/blog\/author\/oopus\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Debugging Pitfalls: Async forEach and Slice Memory Sharing in JavaScript - Oopus Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Debugging Pitfalls: Async forEach and Slice Memory Sharing in JavaScript - Oopus Blog","og_description":"From last Thursday until just now (Saturday), two issues confused me a lot: forEach is synchronous, not asynchronous This is a fundamental concept, but problems can arise when you assume a block of code will execute synchronously while using forEach. For some reason, if that block of code becomes asynchronous, it could lead to unexpected [&hellip;]","og_url":"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/","og_site_name":"Oopus Blog","article_published_time":"2024-11-23T23:25:41+00:00","article_modified_time":"2024-11-24T05:47:06+00:00","author":"oopus","twitter_card":"summary_large_image","twitter_misc":{"Written by":"oopus","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/#article","isPartOf":{"@id":"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/"},"author":{"name":"oopus","@id":"https:\/\/www.oopus.info\/blog\/#\/schema\/person\/0491ca0bc5ba1f23b62bb7be621f29c9"},"headline":"Debugging Pitfalls: Async forEach and Slice Memory Sharing in JavaScript","datePublished":"2024-11-23T23:25:41+00:00","dateModified":"2024-11-24T05:47:06+00:00","mainEntityOfPage":{"@id":"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/"},"wordCount":96,"articleSection":["computer science"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/","url":"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/","name":"Debugging Pitfalls: Async forEach and Slice Memory Sharing in JavaScript - Oopus Blog","isPartOf":{"@id":"https:\/\/www.oopus.info\/blog\/#website"},"datePublished":"2024-11-23T23:25:41+00:00","dateModified":"2024-11-24T05:47:06+00:00","author":{"@id":"https:\/\/www.oopus.info\/blog\/#\/schema\/person\/0491ca0bc5ba1f23b62bb7be621f29c9"},"breadcrumb":{"@id":"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.oopus.info\/blog\/2024\/11\/23\/foreach-and-shadow-copy-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.oopus.info\/blog\/"},{"@type":"ListItem","position":2,"name":"Debugging Pitfalls: Async forEach and Slice Memory Sharing in JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/www.oopus.info\/blog\/#website","url":"https:\/\/www.oopus.info\/blog\/","name":"Oopus Blog","description":"Oopu&#039;s self-recording","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.oopus.info\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.oopus.info\/blog\/#\/schema\/person\/0491ca0bc5ba1f23b62bb7be621f29c9","name":"oopus","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/e6169b542ea65ef728fae42c117aa13b4897842f8263161765d1384a2a6e6fd9?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/e6169b542ea65ef728fae42c117aa13b4897842f8263161765d1384a2a6e6fd9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e6169b542ea65ef728fae42c117aa13b4897842f8263161765d1384a2a6e6fd9?s=96&d=mm&r=g","caption":"oopus"},"sameAs":["http:\/\/www.oopus.info\/blog"],"url":"https:\/\/www.oopus.info\/blog\/author\/oopus\/"}]}},"_links":{"self":[{"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/posts\/1199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/comments?post=1199"}],"version-history":[{"count":5,"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/posts\/1199\/revisions"}],"predecessor-version":[{"id":1204,"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/posts\/1199\/revisions\/1204"}],"wp:attachment":[{"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/media?parent=1199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/categories?post=1199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/tags?post=1199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}