Code efficiency and performance

Please forgive the novice Javascripter query, but I’m curious about how best to learn what makes for efficient, high-performance code, both in Javascript and also in general?

As an example I take a classic algorithm scripting challenge (aka Sum All Numbers in a Range): Create a function which returns the sum of all consecutive integers between two values (passed as an array).

My immediate approach to this was to recognise that there’s an extremely simple algebraic formula which takes care of this for any two values (a, b, where a>b):
(To mitigate against cases where b>a you may include math.abs).

Essentially, the function can be reduced to a single, simple line of code.
But does this mean that such a solution is inherently more efficient or better performing than another which may take a more circuitous route?

Four solutions are provided in the FCC guide to the challenge, all of which depend on recursion, for loops or slightly cumbersome counting mechanisms.
Are these solutions better or faster than the one I found (and if so, how would I know)?

your solution is the most performant, but is also a mathematical trick that is not widely known, and a programmer is not expexted to know it

for knowing if an algorithm is performant or not, part of it is the Big O notation, you could start with the Wikipedia article on Big O notation, but there are courses and such

it is not the only part of algorithm performance, but it can be a starting point


Thanks, that’s good to know. I have an interest in maths but I’m no expert.
I’ll read up on Big O Notation!

Yeah, I think you can also get carried away by worrying too much about efficiency. The old Knuth quote is:

The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming.

Unless you are dealing with very big numbers or very complex operations, it usually doesn’t matter. This is especially try in web dev where the computer spends 99% of the time waiting for the user to do something anyway.

My rule of thumb on efficiency is, make smart choices but don’t worry about it until it becomes a problem.

It’s still good to learn about and to be aware, but don’t obsess. Yeah, in the real world, your mathematical solution is better. But the exercise was to learn something about that algorithmic approach.

It’s like in (American) football, when the players are at practice and they push that big heavy sled down the field. Yes, if their goal is to get to the other side of the field, that is a very inefficient way to do it. But if the goal is to train their muscles, there is value in that.

1 Like

Well the formula is much more efficient: it’s a very simple maths operation, which is what computers do. But it’s an optimisation.

The aim at this stage is trying to get you to understand programming logic. IRL, if it was important that that specific function ran as fast as possible, then definitely. If it isn’t, it may be clearer to someone reading who wasn’t aware of the trick if it was written another way. It’s also difficult to modify – this is the issue with optimisations: if you need to change what it does even slightly, it’s harder to that the more optimised it is.

Note that if it’s a utility function, then how the function is implemented internally shouldn’t make much difference as long as it takes the expected input and returns the expected output. If it was written using a different approach, and it needed to be optimised, you just replace the internals of that specific function with the formula version and that’s you done.

1 Like

Those are really useful points to consider, thank you.
FWIW, with every algorithm scripting challenge, I try to find a good solution and pass the challenge and then I go through all of the provided solutions and figure out how they work too. So far I’ve taken 62pp of notes on this course alone. I never move on until I feel that I fully understand how the code is working. It took me about three days to get my head around exactly how recursion works for instance!

1 Like

There are ways of measuring and analyzing different types of efficiencies, but what I would focus on is looking at code you write and asking two questions:

  • Am I doing things I don’t need to do?
    • You’ll often end up with bits of unecessary calculations, comparisons, or bits of logic that are remnants of your problem-solving process.
  • Am I doing anything more often than I need to?
    • You’ll sometimes discover that you were re-evaluating something that you’ve already figured out once and can reference, or maybe creating two loops when it’s possible to do the logic in one, etc.

That seems a really good approach. The algorithms are good practice for learning specific things about a language. And you can keep going back to them as well, they’re small puzzles for which there are generally any number of ways to solve them. There’s a jump from going from that to a whole system, but this will help a lot.

1 Like

Yeah, not only is that a good way to learn, but that is a good way to approach coding problems on interviews - find a quick and dirty solution and then say, “That works but I think I can make it better…” Too many people try to go for the “impressive” solution and run out of time.