DEV Community

Cover image for Maximum JavaScript Performance
K
K

Posted on

Maximum JavaScript Performance

Cover image by Fachstelle für Öffentliche Bibliotheken NRW on Flickr.

If you're a bit like me you probably think a lot about how to make your software even better. One big point of cause is, to increase the performance TO THE MAX. Today I'll tell you the well kept performance secrets senior JS devs hiding from you.

1. Global variables only

Shadowing clutters your metal model of the software and often you need access to stuff you didn't anticipate when starting your project. If you always reuse your global namespace, you can be safe not to clutter your memory.

// instead of 
const a = "Hello";
const b = "World";

// write
a = "Hello";
b = "World";
Enter fullscreen mode Exit fullscreen mode

2. Short variable names

Never go over board with the length of a variable, parsing time is a big problem in modern JavaScript, so only use more than one letter for a variable name when your global namespace is finally full.

// instead of 
loginTimeout = 2300;

// write
a = 2300;
Enter fullscreen mode Exit fullscreen mode

3. Don't use functions

Functions are the root of all evil! Not only do they add mental overhead to your code by cluttering it with indirections, they also slow it down. So when in doubt, always in-line your code.

// instead of
const substract = (x, y) => x - y;
a = substract(2000, substract(500, 50));

// write
a = 2000 - (500 - 50);
Enter fullscreen mode Exit fullscreen mode

4. setTimeout all the way

Often you need to get stuff done quickly and the synchronous way is just not what cuts it. The best thing to do is replacing all the loops with setTimeout calls, so the event-loop takes care of everything.

// instead of
let x = 1; 
while (x < 1000) {
  console.log(x);
  x++;
}

// write
x = 1;
const fastLoop = () => {
  console.log(x);
  x++;
  if (x < 1000) setTimeout(fastLoop, 1);
}
fastLoop();
Enter fullscreen mode Exit fullscreen mode

5. Object access through with

If you've written any amount of meaningful JavaScript code in your life, you probably ended up with huge objects that you need to dig deep into. Would't it be nice to get rid of all these dots operators that slow you down while you code? Well the with operator got you covered here. Less code to parse means faster code.

// instead of 
console.log(u.profile.name);
console.log(u.profile.birthday);
console.log(u.profile.town);

// write
with(u.profile) {
  console.log(name);
  console.log(birthday);
  console.log(town);
}
Enter fullscreen mode Exit fullscreen mode

7. Cached randomness

Get yourself to the next tabletop shop and buy a bag of dice. They will help you to get random values you can cache in constants for later use. Calculating random values is rather slow and should be done static.

// instead of 
myRandomNumber = Math.random();

// write
r = 17;
Enter fullscreen mode Exit fullscreen mode

Conclusion

There are many easy ways to speed up your code that even starters can follow. It's already the 1st of April 2018, so I don't understand why we should settle with slow software.

April Fool's!

Oldest comments (30)

Collapse
 
ben profile image
Ben Halpern

Collapse
 
efleurine profile image
Emmanuel

A lovely one

Collapse
 
damcosset profile image
Damien Cosset

Holy fuck, it took me way too long to realize this was a joke :D

Well done, sir!

Collapse
 
lluismf profile image
Lluís Josep Martínez

Translate to spanish and post it december 28th.

Collapse
 
joelnet profile image
JavaScript Joel

slow clap

Collapse
 
pmcgowan profile image
p-mcgowan • Edited

I went through the first first one thinking I'm not so sure about that, does it really increase performance though? Got to the second one and thought I guess this makes sense, but readability-wise no... "no more than two letters"... ohhh, I see what's going on here.

Collapse
 
henryjw profile image
Henry Williams

Thanks for sharing. I've noticed a 3x performance gain after implementing these changes!

Collapse
 
kayis profile image
K

Next: Delete system32

Collapse
 
avalander profile image
Avalander • Edited

Hilarious!

The well kept performance secrets senior JS devs hiding from you <- This should have been the title.

Collapse
 
brianaruff profile image
Brian Ruff

Kne it as soon as I saw global variables lol.

Collapse
 
dgubert profile image
Douglas Gubert • Edited

I was reasoning about how deferring things to the event loop like that would make things better when I realized the date this article was published xD

And, tbh, I only started really doubting things when I got to the with section :P