DEV Community

[Comment from a deleted post]
Collapse
 
bgadrian profile image
Adrian B.G.

I know but that is not an excuse to write more complex code that is less efficient and consumes more memory, in my opinion of course. 3 extra stacks and 5 variables will not affect the 2GB Chrome memory hog of course, but we have to start writing better code eventually.

Also providing some Defensive programming in these kind of articles would help raise awareness among the web developers, for example at least check if the collection is an array or a string and throw exceptions if something is malformed or missing.

I know that is only an example but junior developers only see this kind of code, and will have the false assumption that the production code should look like this too.

 
daanwilmer profile image
Daan Wilmer

We have to start writing better code eventually

What, exactly, is better code? Is it optimized for readability and understandability, or for performance?

In my opinion, writing your code in a clear and concise way should be step one, and only change the code for more performance if it is necessary. With the binary search function in Javascript, there are so many things going on that this would be nowhere near the top of things to optimize (except maybe in very exceptional cases).

If things work slow, it's a good skill to know how to optimize for performance โ€” but it is at least as important to know which parts take the most time and prioritizing what to work on first. And my guess is that in most cases, the binary search isn't what will take the most time.

 
bgadrian profile image
Adrian B.G.

What, exactly, is better code? Is it optimized for readability and understandability, or for performance?

In this case is none of them, like I said in my first comment, it is a non-standard implementation (recursive, while most of the examples I could find are iterative), and less performant.