Today I Learned - Rocky Kev

TIL what matters for JS optimization

POSTED ON:

TAGS:

Two years ago, I remember arguing with a coworker over what loops to use for optimization. (Spoiler: congrats on saving milliseconds on something that might be optimized a few months later)

Via Writing high-performance JavaScript

When writing High Performance Javascript, here's doesn't matter for Javascript Optimization:

Doesn't matter:

Using getElementById() vs querySelector():

For example, you may read that document.getElementById() is more than twice as fast as document.querySelector(), and start fretting about which selector method to use when.

It’s true, by the way. The document.getElementById() method can run about 15 million operations a second, compared to “just” 7 million per second for the document.querySelector() method in the latest version of Chrome. But that also means that the document.querySelector() method runs 7,000 operations a millisecond. That’s really damn fast!

Loops:

I also similarly see people worry about things like which loop method is faster.

Here's a blog post with data: https://blog.bitsrc.io/measuring-performance-of-different-javascript-loop-types-c0e9b1d193ed

And similar to the above comment... imagine shaving fractions of fractions of fractions of seconds off.

There's way better optimization you can look at.

Matters


Related TILs

Tagged:

TIL what is npm Script

Despite their high usage they are not particularly well optimized and add about 400ms of overhead. In this article we were able to bring that down to ~22ms.

TIL fancy methods to transform Javascript Objects

You can use Object.entries(), Object.keys(), Object.fromEntries()...

TIL how to hide your JS code

ONE THING TO NOTE: Encrypting a script is stronger than obfuscation, both methods are still not adequate to protect secret content. Honestly, I don't think it's worth it on a production site, and instead just go with pure server-side if you want security. But it's fascinating.