What is the most efficient way to deep clone an object in JavaScript?

Native deep cloning There’s now a JS standard called “structured cloning”, that works experimentally in Node 11 and later, will land in browsers, and which has polyfills for existing systems. structuredClone(value) If needed, loading the polyfill first: import structuredClone from ‘@ungap/structured-clone’; See this answer for more details. Older answers Fast cloning with data loss – … Read more

How do I replace all occurrences of a string in JavaScript?

As of August 2020: Modern browsers have support for the String.replaceAll() method defined by the ECMAScript 2021 language specification. For older/legacy browsers: function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, ‘\\$&’); // $& means the whole matched string } function replaceAll(str, find, replace) { return str.replace(new RegExp(escapeRegExp(find), ‘g’), replace); } Here is how this answer evolved: str = … Read more

How do I resolve merge conflicts in a Git repository?

Try: git mergetool It opens a GUI that steps you through each conflict, and you get to choose how to merge. Sometimes it requires a bit of hand editing afterwards, but usually it’s enough by itself. It is much better than doing the whole thing by hand certainly. As per Josh Glover’s comment: [This command] … Read more

What is a plain English explanation of “Big O” notation?

Quick note, my answer is almost certainly confusing Big Oh notation (which is an upper bound) with Big Theta notation “Θ” (which is a two-side bound). But in my experience, this is actually typical of discussions in non-academic settings. Apologies for any confusion caused. BigOh complexity can be visualized with this graph: The simplest definition … Read more

How can I safely create a nested directory?

On Python ≥ 3.5, use pathlib.Path.mkdir: from pathlib import Path Path(“/my/directory”).mkdir(parents=True, exist_ok=True) For older versions of Python, I see two answers with good qualities, each with a small flaw, so I will give my take on it: Try os.path.exists, and consider os.makedirs for the creation. import os if not os.path.exists(directory): os.makedirs(directory) As noted in comments … Read more

How can I validate an email address in JavaScript?

Using regular expressions is probably the best way. You can see a bunch of tests here (taken from chromium) const validateEmail = (email) => { return String(email) .toLowerCase() .match( /^(([^<>()[\]\\.,;:\s@”]+(\.[^<>()[\]\\.,;:\s@”]+)*)|(“.+”))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ ); }; Here’s the example of a regular expression that accepts unicode: const re = /^(([^<>()[\]\.,;:\s@\”]+(\.[^<>()[\]\.,;:\s@\”]+)*)|(\”.+\”))@(([^<>()[\]\.,;:\s@\”]+\.)+[^<>()[\]\.,;:\s@\”]{2,})$/i; But keep in mind that one should not rely … Read more

For-each over an array in JavaScript

TL;DR Your best bets are usually a for-of loop (ES2015+ only; spec | MDN) – simple and async-friendly for (const element of theArray) { // …use `element`… } forEach (ES5+ only; spec | MDN) (or its relatives some and such) – not async-friendly (but see details) theArray.forEach(element => { // …use `element`… }); a simple … Read more

What is the maximum length of a URL in different browsers?

Short answer – de facto limit of 2000 characters If you keep URLs under 2000 characters, they’ll work in virtually any combination of client and server software. If you are targeting particular browsers, see below for more details on specific limits. Longer answer – first, the standards… RFC 2616 (Hypertext Transfer Protocol HTTP/1.1) section 3.2.1 … Read more