DEV Community

[Comment from a deleted post]
Collapse
 
thejoezack profile image
Joe Zack

Setting the length to 0 hurts my brain, but it works!

Collapse
 
sait profile image
Sai gowtham

yeah it works but performance is poor

Collapse
 
chenmike profile image
Mike Chen

Genuine question: do you feel like performance is even a concern here? The difference between these operations is probably literally nanoseconds unless you have enormous arrays. What do you think of the readability/code clarity trade-offs between these options?

 
tinussmit profile image
Tinus Smit

In my opinion, yes.

Performance should always be a concern. Sure, in a single example like this, it seems petty to harp on that one has to use the fastest code ever. But remember that code will almost always be a series of statements.

Consider if the code was littered with the least efficient versions of operations. Methods would take longer to complete, and thus the application would appear slow. And isn't the ultimate goal of software to help humans do things faster (as fast as possible)? :-)

 
chenmike profile image
Mike Chen • Edited

I agree that all things being equal performance should be important. Things are rarely equal though, and in this case I was making a point that I feel that readability matters much more in this situation.

Put another way: people are definitely going to have to read your array clearing code if you write it, so readability is going to be a concern. How many of these array clearing operations do you think it would take to make a noticeable difference in your app's performance? I feel like it would have to be millions.

 
moopet profile image
Ben Sinclair

I come from the generation where every byte counted and I know that optimisation sometimes seems like a lost art, but having something that you can understand without having to scratch your head for a second is more important in almost every situation.

Collapse
 
tinussmit profile image
Tinus Smit

I know, right. In my entire career, length has always been seen as a read-only property. Being able to set it seems like we're eventually able to lie about the true content of the array :-P