/r/javascript

Photograph via //r/javascript

Chat about javascript and javascript related projects. Yes, typescript counts. Please keep self promotion to a minimum/reasonable level.

All about the JavaScript programming language.

Subreddit Guidelines


Specifications:


Resources:


Related Subreddits:

r/LearnJavascript

r/node

r/typescript

r/reactjs

r/webdev

r/WebdevTutorials

r/frontend

r/webgl

r/threejs

r/jquery

r/remotejs

r/forhire


/r/javascript

2,397,024 Subscribers

2

[AskJS] env-protector: A robust environment variables validator for Node.js with TypeScript support

Hi everyone,

I’ve just released a package: env-protector. It’s a tool I built to help with validating environment variables in Node.js applications, especially with TypeScript support.

Why did I create this?

In many Node.js projects, environment variables are crucial, but handling them can be error-prone. I found that:

  • Environment variables are often manually validated in various parts of the code, leading to repetitive logic.
  • There was no easy way to enforce types and validation rules on environment variables.
  • Debugging issues caused by missing or invalid environment variables was tedious.

So, I decided to create env-protector to simplify this process. It offers:

  • Type validation for environment variables (string, number, boolean, url, email, etc.).
  • Support for custom validation rules using regex patterns or enumerated values.
  • Detailed error messages to quickly identify issues with your environment setup.
  • Full TypeScript support for a safer development experience.

Key Features:

  • Flexible type validation: Ensure your environment variables are of the correct type and format.
  • Custom rules: Set specific validation rules for variables using patterns or predefined values.
  • Clear error messages: Understand exactly what went wrong if your variables don’t pass validation.
  • TypeScript integration: No more worrying about type mismatches with full TypeScript support.

If you’ve ever struggled with managing and validating environment variables in your Node.js projects, this tool might be useful for you!

🔗 NPM Package: env-protector on npm
💻 GitHub Repo: env-protector GitHub

0 Comments
2024/12/03
06:38 UTC

0

[AskJS] Any polyfill library to use TC39 Signals?

https://github.com/tc39/proposal-signals https://www.npmjs.com/package/@lit-labs/signals

Lit has implemented signals in labs, but I want to use signals in vanilla JS.

15 Comments
2024/12/02
10:43 UTC

7

[AskJS] Any local database like pocketbase?

I want to do a project with electron, but I don't know how to manage the data, I like pocketbase for the authentication, file storage and realtime but every time I have to use it I have to use the command “pocketbase serve” and it is not comfortable for the end user, maybe use sqlite or what other option do you recommend, if sqlite which package or which client do you recommend?

I would like that the end user does not have to configure anything, just install a . exe and that's it.

10 Comments
2024/12/02
04:59 UTC

0

[AskJS] What specifcally is exploitable about and how would you exploit node:wasi?

Node.js' node:wasi modules includes disclaimers such as

The node:wasi module does not currently provide the comprehensive file system security properties provided by some WASI runtimes. Full support for secure file system sandboxing may or may not be implemented in future. In the mean time, do not rely on it to run untrusted code.

and

The current Node.js threat model does not provide secure sandboxing as is present in some WASI runtimes.

While the capability features are supported, they do not form a security model in Node.js. For example, the file system sandboxing can be escaped with various techniques. The project is exploring whether these security guarantees could be added in future.

11 Comments
2024/12/01
14:34 UTC

7

[AskJS] Reducing Web Worker Communication Overhead in Data-Intensive Applications

I’m working on a data processing feature for a React application. Previously, this process froze the UI until completion, so I introduced chunking to process data incrementally. While this resolved the UI freeze issue, it significantly increased processing time.

I explored using Web Workers to offload processing to a separate thread to address this. However, I’ve encountered a bottleneck: sharing data with the worker via postMessage incurs a significant cloning overhead, taking 14-15 seconds on average for the data. This severely impacts performance, especially when considering parallel processing with multiple workers, as cloning the data for each worker is time-consuming.

Data Context:

  1. Input:
    • One array (primary target of transformation).
    • Three objects (contain metadata required for processing the array).
  2. Requirements:
    • All objects are essential for processing.
    • The transformation needs access to the entire dataset.

Challenges:

  1. Cloning Overhead: Sending data to workers through postMessage clones the objects, leading to delays.
  2. Parallel Processing: Even with chunking, cloning the same data for multiple workers scales poorly.

Questions:

  1. How can I reduce the time spent on data transfer between the main thread and Web Workers?
  2. Is there a way to avoid full object cloning while still enabling efficient data sharing?
  3. Are there strategies to optimize parallel processing with multiple workers in this scenario?

Any insights, best practices, or alternative approaches would be greatly appreciated!

25 Comments
2024/11/30
20:52 UTC

0

[AskJS] Looking for a Modern, Performant JavaScript UI Library for IE10/Chakra on Xbox (No Build Tools, No jQuery)

Hi folks,

I'm forced to maintain an Xbox app written in JavaScript(WinJS) that runs on the internal EdgeHTML Chakra engine of Xbox (based on IE10). The app currently uses WinJS, which is now obsolete.

I’m planning to gradually rewrite the UI with a modern, fast library that:

  1. Supports IE10/Chakra.
  2. Doesn’t require build tools (so we can debug directly in VS2017). as the Xbox browser can only be accessed within VS too.
  3. Allows incremental changes without overhauling the entire app (so can't use WebView2). The JS are using the transparent exposed WinRT APIs which is done automatically by the Xbox browser
  4. Avoids jQuery. I want the most modern I can get here and jQuery is not really maintainable.

I’ve been considering React 16.8 or 17, but I’m concerned it might slow things down on IE10. Ideally, I want something with React’s cleanliness but optimized for performance on IE10. It doesn't have to be React but It has to be modern. WebComponents is not an option as its not properly supported in IE10 more so on this lobotomized IE10.

Any recommendations or advice? Thanks in advance!

9 Comments
2024/11/30
17:48 UTC

8

[AskJS] Program Design, OOP, JavaScript

Are there senior programmers who's first (and maybe only) language is JS/TS.

I started with JS as a first language, so I built all my knowledge around it. Mostly I'm interested in backend and node. I'm at the point where I want to build knowledge about software design, and as I don't know any other language, the problem is I always see resources/books about oop, patterns, architecture and so on utilising Java or C# or other typed language with OOP paradigm.

Software design is not about the language, and there are even resources for JS/TS, but the way things implemented in JS world are different from more traditional languages.

And I have doubts if I learn it only JS way, I will always have an impostor syndrome.

So basically the question, are there successful developers (backend) without background in any other language before JS, and how you got your Software Design knowledge. And can you easily transfer it to other languages?

15 Comments
2024/11/30
14:43 UTC

2

Showoff Saturday (November 30, 2024)

Did you find or create something cool this week in javascript?

Show us here!

6 Comments
2024/11/30
08:00 UTC

5

[AskJS] What do you think about lazily evaluated objects?

Like those objects with values and even property names computed on the fly, but take it a step further. None of the supposed fields of the object exist in memory yet, and only when you access them they are evaluated and created on the object once.
For a simple example:
You expect a function to return an array with a step condition, so it would be something like [0,2,4,6,8,10] for a step = 2. We don't actually have to store all the indeces in memory (could be thousands of numbers). We could have an object that appears to have obj[2] as 4 or obj[4] as 8 or obj[7] as undefined (not created) while we really only create those properties when we look at them.

The object will be very ligthweight even with thousands of expected properties, it will trade speed of intant access to predefined properties for memory efficiency of literally not having those properties untill you need each of them, could be used in phone apps.

Edit: computed, not evaluated properties, so far I don't know how to compute properties for generic objects in order to lazily evaluate them.

Edit2: by storing only important information of a predictable sequence we can remove 2 things:

  1. upfront cost for calculating all entries of a sequence.
  2. upfront cost for storing the entirety of a calculated sequence.

While still maintaining the ability to access random parts of the sequence as if it were present.
After getting some examples from Ruby I went from using a Proxy to using a class with a method.
I have done some measuring at length 1000 for getting a property in a loop and adding it to a variable:

  • a lazy array made the loop ~5x slower than a normal array
  • a lazy array that recorded properties after they have been looked at made the loop ~1.5-2x slower than a normal array

I'd say this is an acceptable speed loss in favour of not creating upfront and storing the entire sequence, takes less memory to keep and less time to initialize. Of course such an abstraction so far only works on predictable sequences.

43 Comments
2024/11/29
13:20 UTC

0

[AskJS] Suggestions for JS code editors

Hi guys,

So I'm working as an intern using my own laptop. I use VSCode for my company's codes. I heard about the free non-commercial version of WebStorm being release about a month ago and started using it. But recently it's crashing a lot and also crashed my windows a few times also.

Also recently I started working with BUN. Is that probably causing issues?

So what I want to get suggestion is about a whole new code editor or another method such that I can split the VSCode into two modes, work / personal.

Windows suggestions please.

*If mac suggestions are there please give them also as I'm planning a buy a second hand one. PS: BTW what do you guys think about second hand macs.

14 Comments
2024/11/29
11:55 UTC

9

[AskJS] What's the tech stack you would recommend me for landing pages with 3D?

Hi everyone,

I’m a newbie when it comes to web development, though I have programming experience. I’m diving into web development and want to learn how to create small landing pages with 3D components and animations.

Here’s what I’ve been considering so far:

  • Astro + React Three Fiber + React Rapier

I’m debating whether I should go with Astro/Vite or Next.js as my framework. I’m fairly confident about sticking with React Three and Fiber for the 3D elements.

What are your thoughts? Any insights or suggestions would be appreciated!

Thanks! 🙌

9 Comments
2024/11/28
13:55 UTC

0

[AskJS] We are in 2024. Do we still need the semi-colon or not?

Hello people. I consider myself a backend developer, but I have to write a lot of JS as well. As an outsider of the JS trends, I am wondering if there is any update on the use (or not) of the semi-colon to set the end of the command.

Are you still adding them? Yes, no, why?

Thanks

15 Comments
2024/11/28
13:08 UTC

1

[AskJS] Node.js or Python for API development like for a 2-in-1 Soccer News & Player Profiles API?

Hey everyone!

So I recently began working on a 2-in-1 Soccer News & Player Profiles API – an API where people can access player profiles AND the latest soccer news from trusted sources, like AP News, FIFA, Goal, ESPN, CBS Sports and more in an instant.

It's a REST API, and offers full support for creating, reading, updating, and deleting player profiles. In that regard, whether you're building a simple player database or a complex application, you can seamlessly manage every aspect of player data with the API's comprehensive CRUD capabilities.

I made it out of Node.js, but I'm unsure if I should continue using it for future projects due to being more proficient in Python than Node.js. What makes Node.js better than Python, and in what scenarios is it typically preferred over Python?

2 Comments
2024/11/28
04:08 UTC

17

[AskJS] Beginners: What do you struggle with when learning JavaScript?

I'm thinking of writing an eBook on JavaScript aimed at mitigating common JavaScript pain points for beginners and demystifying what's actually simple.

Newbies: what are you struggling to learn at the moment?

32 Comments
2024/11/28
01:42 UTC

2

[AskJS] Dart or JavaScript

Hello programmers,

I have a question for the community. I am looking for some input into whether I should learn JavaScript or Dart as a first programming language. I am mainly looking to learn to develop apps in th far future, and maybe pick up some freelance work after I get some experience.

I am trying to decide if Dart/Flutter or JavaScript/React Native is the better first set of language and tool to learn before I totally dive in.

I know FreeCodeCamp and Odin Project have great tools for learning JavaScript, and I do have a book on Dart. Any input is appreciated!

9 Comments
2024/11/27
13:18 UTC

Back To Top