RSSAmplifier

Blog

8 Hobbies JavaScript Blog

Recent content on 8 Hobbies JavaScript Blog

8hob.ioRSS feed ↗27 posts

Latest posts

Does SIMD Improve the Performance of Node.js?

Single instruction, multiple data (SIMD) instructions , such as AVX on x64 processors,
are designed to improve performance. In some areas, such as numerical computation, they offer
significant performance gains. In some other areas, they provide less gain or even performance loss. 
 Do they improve the performance of Node.js? We ran some experiments.

Add the Current Copyright Year to the HTML File in a Vite Project

In a project built by Vite , index.html is usually the entry point of the
project . While we can put the copyright notice
in the JavaScript bundle, it is often desirable to add it directly to the HTML file for various
reasons. For example, maybe the copyright notice is entangled with the privacy policy or terms of
service links, which we usually want to make visible even when the…

Guides to Promoting a JavaScript Open Source Project

You just have created a new JavaScript open source project. Now you wonder, how do I
promote it? Having been devoted to open source for over a decade, I’d like to share how I usually
approach it. Many of these ideas apply to not only JavaScript projects but also open source
software projects in general. 
 There are 4 aspects to focus on: 
 
 Value Addition 
 High…

Add/Update/Remove an HTML Attribute in JavaScript

HTML attributes are name-value pairs, separated by = , in the start tag of an element after the&#xA;element name, such as id=blog-link and href="https://8hob.io" below: &#xA; < a id = 'blog-link' href = 'https://8hob.io' > 8 Hobbies JavaScript Blog </ a > &#xA; How do we add, update, and remove an HTML attribute in JavaScript (JS)?

Simple and Error-Free Linked List Implementation With a Dummy Head/Tail Node

Linked lists are fundamental data structures frequently used in computer science. Often,&#xA;programmers need to directly implement them. How can we implement an error-free linked list quickly,&#xA;especially under time pressure and mental stress during job interviews ? This post&#xA;discusses an important trick: Using a dummy head/tail node .

A Type-Safer Alternative to Type Guards in TypeScript

TypeScript allows programmers to define type guards so that programmers have more control&#xA;over the dynamic nature of the typing of JavaScript. Since type guards are completely defined by the&#xA;programmer, they also evade type checking from the TypeScript compiler: Type checking won&rsquo;t catch&#xA;any error if the type guard doesn&rsquo;t correctly determine the type. This can be caused by…

Should You Build Your Next Command Line Utility Using Node.js? Factors to Consider

&#xA; Traditionally, C, C++, and Python are popular choices for writing command line utilities . With&#xA;the rise of Node.js , many people have turned to Node.js for writing command line utilities.&#xA;Should you base your next command line utility on Node.js? We believe it&rsquo;s a good idea to follow a&#xA;holistic approach that considers multiple factors. In this post, we discuss the factors…

Bitwise Operators in JavaScript and When to Use Them

&#xA; Bitwise operators in JavaScript are fundamental to the language. These operators apply logical&#xA;operations to each bit of the operands. They consist of bitwise NOT ( ~ ), bitwise AND &#xA;( & ), bitwise OR ( | ), and bitwise XOR ( ^ ), as well as their assignment variations &= ,&#xA; |= , and ^= . This post discusses what they are and the best practices around them. &#xA; The shift…

Javascript/Python Array Quick Reference

Whenever I&rsquo;m learning or using an unfamiliar programming language, I often find that even some&#xA;common tasks can become overwhelming. A quick reference table that refers back to my familiar&#xA;programming language often helps me carry over tricks that I already know, at least initially. &#xA; The tables below are intended to help JavaScript/Python programmers learn and quickly find…

Assertions in Loops in Unit Tests: Tips and Best Practices

Assertions, such as expect in vitest and jest , are an indispensable part of unit tests.&#xA;However, assertions inside loops risk being silently skipped. In this post, we discuss some tips&#xA;and best practices to avoid situations where assertions live in loops.

Assertions in If-Clauses in Unit Tests: Tips and Best Practices

Assertions, such as expect in vitest and jest , are an indispensable part of unit tests.&#xA;However, assertions inside if -clauses risk being silently skipped. There is even an ESLint rule&#xA; no-conditional-expect that checks conditional&#xA;assertions .&#xA;In this post, we discuss some tips and best practices to avoid situations where assertions live in&#xA; if -clauses.

An Elegant and Safe Solution for the Strict Typing of Array.includes

Array.includes is a commonly used function in JavaScript . However, in TypeScript , its&#xA;typing is quite strict: The element being searched for must have the same type as that of the array&#xA;element .&#xA;This causes type errors if the array is of a literal type. For example, with the following code&#xA;snippet: &#xA; const okNumbers = [ 1 , 3 , 5 , 7 ] as const ; &#xA; console . log ( `2 is…

JavaScript Performance Tips: The Hidden Cost of Literals

A literal is a textual representation (notation) of a value as it is written in source code.&#xA;Many people generally associate the concept of literals with performance &ldquo;cheapness&rdquo;: A literal&#xA;always seems to consume very little resource. Is this true? This post discusses the hidden&#xA;performance cost of literals in JavaScript.

Tips to Improve Git Experience for JavaScript Projects

Git is an open source distributed version control system . It is currently the most popular&#xA;version control system according to various surveys ,&#xA;and has been the core driver of many popular development platforms, such as GitHub , GitLab ,&#xA; Bitbucket , etc. &#xA; Many JavaScript developers also use Git as their main version control system. Therefore, improving&#xA;Git experience is key…

HTML Tricks to Speed up Loading React Apps

Besides many tricks that improve the performance from within a React&#xA;app , there are also&#xA;tricks to improve the loading speed from the HTML document in which the React app is embedded.&#xA;In this post, we will walk through some of these tricks.

Semantic Versioning of Npm Packages After a Dependency Update

Semantic Versioning (semver) is a versioning scheme that is officially recommended by&#xA;npm . It recommends every package to be versioned&#xA;in the format MAJOR.MINOR.PATCH and to follow 3 basic principles of incrementing/bumping (Quoted&#xA;from https://semver.org/ ): &#xA; &#xA; &#xA; MAJOR version when you make incompatible API changes; &#xA; MINOR version when you add functionality in a…

TypeScript: Typing CSS Variables in React

React allows specifying the style of an HTML element from within the&#xA;app , which is a&#xA;powerful tool when the style depends on the data or logic of the app. On the other hand, CSS&#xA;variables , also known as CSS custom properties, provide great flexibility for CSS . &#xA; For example, the following code specifies a CSS variable of the h1 element: &#xA; export default function App () {…

Guides to Writing Cross-Platform Scripts in package.json

Node.js allows defining scripts in&#xA; package.json . This feature often constitutes a&#xA;major component of a developer&rsquo;s workflow. For example, developers can define procedures such as&#xA;building, testing, and deployment as scripts in&#xA; package.json . If the project is&#xA;intended to be cross-platform, the scripts should also be cross-platform. &#xA; In this post, we discuss…

Automatic Polyfill for Modern Browsers With Vite

Vite is a modern tool for frontend development. One of Vite&rsquo;s functionalities is&#xA;to deploy the app for production , including bundling all&#xA; JavaScript (JS) files. Although Vite supports building for a particular browser&#xA;target , it only transforms syntax with the help of&#xA;ebuild , but does not perform any JavaScript polyfill . &#xA; However, polyfill is essential for…

Exploring the Difference Between the Effects of Dependencies and Peer Dependencies after NPM v7

Npm allows multiple types of dependencies, two of which are&#xA; dependencies and peerDependencies . Semantically, they refer to different things:&#xA; peerDependencies expresses the compatibility of a package with a host tool or library, usually&#xA;referred to as a plugin, while dependencies expresses dependencies in the general sense. &#xA; Despite being two distinct types of dependencies,…

Restrict Access to a JavaScript Application Embedded via an iframe

One common way to embed a JavaScript application in a web page is using&#xA; iframe .&#xA;Assuming the application is accessible from https://example.com/my-app and the embedding page is&#xA; https://example.com/content , the embedding code in the embedding page would look like this: &#xA; < body > &#xA; <!-- Other content... --> &#xA; < iframe src = 'https://example.com/my-app' >< iframe > &#xA;…

Set an Attribute Without Value in JavaScript

Often, we see HTML code snippets that set an attribute of an element without any value, like the&#xA;following: &#xA; < button id = 'my-button' disabled > I'm a button </ button > &#xA; In the HTML code snippet above, the id attribute has a value of "my-button" , while the&#xA; disabled attribute is present without any specified value. Many of us may wonder: How can I add&#xA;such an attribute…

Maintaining an Open Source JavaScript Project? Help Dependents Retain Notices

This post is not legal advice. By reading this post, you agree and acknowledge that this post does&#xA;not advise you on how to properly license your work and you would not rely on this post to license&#xA;your work; that this post does not advise licensees of open source work on how to comply with the&#xA;licenses; and that this post only provides technical guidance on programming tasks that are…

Unleash Git Power with Enhanced User Configuration

Git is an open source distributed version control system . It is currently the most popular&#xA;version control system according to various surveys ,&#xA;and has been the core driver of many popular development platforms, such as GitHub , GitLab ,&#xA; Bitbucket , etc. &#xA; While Git comes with a decent default configuration, it is far from the full power of Git. In this&#xA;post, we will walk…

Make TypeDoc Generate a 404 Page

Motivation &#xA; TypeDoc is a document generator that, among other things, converts comments in TypeScript &#xA;source code into rendered HTML documentation. The generated HTML documentation can be hosted on&#xA;static website hosting services, such as GitHub Pages , GitLab Pages , Netlify , and&#xA; CloudFlare Pages . All of them by default use a 404.html file as the content of the 404 page…

Typing Wrapper Functions in Typescript

Motivation &#xA; Very often in TypeScript , we would like to wrap an existing function into a wrapper function,&#xA;with some additional preprocessing or postprocessing. For example, &#xA; function existingFunc ( arg1 : number , arg2 : string ) : number { &#xA; return arg1 + arg2 . length ; &#xA; } &#xA; &#xA; function wrapperFunc ( arg1 : number , arg2 : string ) : number { &#xA; // Preprocess…

Test the Presence of a Material UI Icon in Jest/Vitest

Testing the presence of a Material UI Icon in Jest / Vitest can be done by using the&#xA; Testing Library . &#xA; First, in the production code, assign a titleAccess attribute to the icon. For example, if the&#xA;icon of interest is AddIcon , the code would look like: &#xA; < AddIcon titleAccess = 'My Add Icon' /> &#xA; This attribute would also surface to the user&#xA;interface .