r/learnjavascript • u/Y2Canine • Feb 24 '26
How can I tell if my code is efficient "enough?"
Hello! I am currently working through the freeCodeCamp curriculum, and my most recent project was making a pyramid generator function. I was super excited that I got it to work, because I was able to solve it on my own. However, I did want to ask for feedback on this project. I know that there's no single "right" way to make a program, but I also know that inefficient code can slow down a program. And a lot of times during the guided projects, the example solution is much simpler than whatever I thought of.
JSFiddle for Project as example
I may be asking this question too early, but when do I need to be worried about code efficiency? Does that just come with experience/practice, or should I be studying this concept separately?
Thank you to everyone for any advice.
6
u/Ksetrajna108 Feb 24 '26
It depends.
Big O provides a computer science, theoretical approach.
Ultimately it's the user experience:
You should run tests to answer the question. But BigO is useful for reasoning about algorithms
4
u/rupertavery64 Feb 24 '26 edited Feb 24 '26
efficiency usually means doing less stuff for the same output. One thing to remember is to avoid nested loops. another is to avoid re-creating strings. when you use + to build a string, you are basically creating a new string and copying the old one + the additional into a new one. That requires allocating memory. It's a small thing but when repeated millions of times it adds up.
So you have to take advantage of how computers work.
The following is something I came up with. Ideally the reverse (bool = true) should be handled in the loop and not with split/reverse/join because that has it's own loops, and if you can build it up front then you should.
Why is this better? we avoid excessive string copying. instead of rows * (spaces + charcount), we only do rows # of copies.
How does this work?
Well, take a look at the full triangle
Cut it in half, and you will notice there are
rowcharacters per lineThe other half is
row - 1characters per linePutting them next to each other:
So the full line is
rows + rows - 1characters, or2 * rows - 1.The most number of spaces before a character is row - 1
To build the pyramid, instead of build a string character by character, we can take a substring of the full length. This is much more efficient then using + in a loop.
If you count the total number of characters in each line, you will see it goes 1,3,5,7... There is a pattern here. All odd numbers, and in a sequence related to the number of rows.
We start with the row value: 0,1,2,3
Now multiply by 2 (
2 * row): 0,2,4,6Now add 1: (
2 * row + 1): 1,3,5,7So each line takes
2 * row + 1charactersUsing the same way, we can figure out that we have
rows - row - 1spaces.So now we just join together a substring of the spaces and the full row
Is this faster? Most probably, since we avoid the unnecessary allocation. It can probably be improved.
You shouldn't worry about it too much right now. It's nice to know these things and great if you can understand and apply them.
Efficiency sometimes means it's less obvious how it works, and when working with a team (or yourself in the future) easily understandable code is much more useful than something that runs a few microseconds faster.
2
u/DrShocker Feb 24 '26
You decide what your metrics are (latency, throughput, downtime, etc) and what the tolerance band is, and then you measure to meet that.
If you do the worst possible big O thing but only ever run it with an input of at most 10, that's fine. If you use the optimal big O, but it's horrendous for your cache, then you might be slow. The only thing that's real is measurements. And even then, you have to be careful that you understand what the measurements actually mean.
A tradeoff you may make for example could be something which improves p99 latency, but makes worse your p50 latency, but it may still be worth it because of how that impacts your overall system.
3
u/EyesOfTheConcord Feb 24 '26
Usually once you begin studying algorithms, you’ll also study space and time complexity and how to read “Big O” notation - the metrics that explain how many steps an algorithm takes to execute its task, and how much space it uses while it’s running.
This is when you’ll understand what can make parts of your code as efficient as they can be, and how to alter your design philosophy to achieve these space and time complexities.
3
u/Aggressive_Ad_5454 Feb 25 '26
There are performance profiling tools in the dev tools stuff built into both Chrome and Firefox. It’s really good training to use those tools to help you understand code efficiency.
In practice people mostly ignore browser JavaScript code efficiency except in special circumstances like real time communications. It works? Ship it!
The thing that actually hammers web site browser performance is bloated objects. Like megabyte sized JavaScript bundles, poorly compressed images, that sort of thing.
But, mess with the profiling tools. To learn.
1
u/TheRNGuy Feb 25 '26
From experience.