Fire tips

These are a collection of tips and tricks you can use to improve the performance and readability of your code.

Math with Infinity

Infinity is just a number, dude. Most calculations JavaScript lets us do with Infinity will still return Infinity. Some no longer return numbers.

JavaScript
Read fire tip

Relative timestamps with Intl

With JavaScript’s built-in formatter for relative timestamps, we can build strings like “2 months from now” without having to use third party libraries.

JavaScript
Read fire tip

Replacing all matches in a string

The String prototype’s replace function only replaces the first occurrence of a substring by default. We can extend that with a global flag on the expression.

JavaScriptregular expressions
Read fire tip

Splitting arrays into chunks

By combining map and slice, we can write a helper function to split large arrays into many similarly sized blocks. This is useful for features like pagination.

JavaScript
Read fire tip

Namespacing styled-components

To improve the readability of React components, we can import the styled-components they use from another file under a ui-namespace.

JavaScriptstyled-componentsreadability
Read fire tip