I've been relying on AI more lately, and I noticed it generated incorrect code when I was working with JavaScript Sets and Maps. It used JSON.stringify and JSON.parse for deep cloning, but that approach loses or mishandles Set and Map data. For modern JavaScript, structuredClone seems like the better option because it supports these data structures directly. Has anyone else noticed AI tools defaulting to outdated cloning patterns, and how do you make them use newer APIs?
3 Answers
JSON.stringify and JSON.parse aren’t suitable for cloning Sets and Maps because JSON doesn’t preserve those types properly. structuredClone is generally the right built-in choice for deep-copying them, as long as the values being cloned are supported by the structured-clone algorithm.
Many language models fall back to familiar older patterns instead of choosing the newest appropriate API. Adding project instructions that explicitly prefer structuredClone and asking the model to explain how it handles special data types can reduce this, but it’s still worth testing the generated code.
This is a good example of why AI-generated code still needs a careful review. Data structures like Set and Map, along with newer platform APIs, are easy places for an AI suggestion to look reasonable while being incorrect.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically