DEV Community

LG
LG

Posted on

2 2

Don't use explicit const in global scope of JavaScript

Global scope should be exploited as little as possible, although if exploited too much (relatively) it's referred to "global pollution" . But if it happened you had no choice, do but just not this way :

const f = {
    namespace: "NS_F"
}
f // {namespace: 'NS_F'}
// let's garbage collect it :
f = null; // Assignment to constant variable
// SOLUTION is to swap CONST with LET, although...
Enter fullscreen mode Exit fullscreen mode

... Although if you a OKAY to garbage collect it as soon as job is done, choose this run-to-completion (functional) approach :

// function declaration
function f () {
    return {namespace: "NS_F"}
}
f() // {namespace: 'NS_F'}
// run the following separately :
f = null;
// run the following separately :
f() // f is not a function
// Hooray !
Enter fullscreen mode Exit fullscreen mode

Nearly best solution is to use weakSet(s) or weakMap(s) – read this


Thanks & see in the next one !

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay