Javascript/Python Array Quick Reference

Whenever I’m learning or using an unfamiliar programming language, I often find that even some common tasks can become overwhelming. A quick reference table that refers back to my familiar programming language often helps me carry over tricks that I already know, at least initially. The tables below are intended to help JavaScript/Python programmers learn and quickly find idiomatic ways to perform certain tasks on lists/arrays in Python/JavaScript. ...

 ·  · 2 min · 355 words

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. However, assertions inside loops risk being silently skipped. In this post, we discuss some tips and best practices to avoid situations where assertions live in loops. ...

 ·  · 2 min · 365 words

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. However, assertions inside if-clauses risks being silently skipped. There is even an ESLint rule no-conditional-expect that checks conditional assertions. In this post, we discuss some tips and best practices to avoid situations where assertions live in if-clauses. ...

 ·  · 3 min · 477 words

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 typing is quite strict: The element being searched for must have the same type as that of the array element. This causes type errors if the array is of a literal type. For example, with the following code snippet: const okNumbers = [1, 3, 5, 7] as const; console.log(`2 is OK? ${okNumbers.includes(2)}`); The TypeScript compiler complains: Argument of type ‘2’ is not assignable to parameter of type ‘1 | 3 | 5 | 7’. ...

 ·  · 6 min · 1133 words

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. Many people generally associate the concept of literals with performance “cheapness”: A literal always seems to consume very little resource. Is this true? This post discusses the hidden performance cost of literals in JavaScript. ...

 ·  · 7 min · 1424 words