4

By optimization, I mean for example deciding between using a basic for loop vs. Array.prototype.forEach() based on which implementation seems to go faster in which browsers, etc. (And in some cases, this can extend to micro-optimization, which they say can be dangerous.)

Here is an example of what could be meant by 'writing for minification':

if(foo === bar) {
    return true;
} else {
   return false;
}

// writing the following instead, which reduces size by a few bytes

return foo===bar; //will return true or false, depending on what the statement evaluates to

Some could argue the using that if else statement is a little more readable at first glance, but I would say that the second way of writing it minifies better.

As far as minification, from what I understand, the big purpose in this is to reduce server response time.

What is the balance between writing for minifcation and server response time vs. readability, vs. writing for 'optimization'?

Edit
One might rephrase the question as writing for optimization vs. writing to make the code 'minify smaller'.

Community
  • 1
  • 1
Josh Beam
  • 19,292
  • 3
  • 45
  • 68
  • 1
    That's not minification, that's just a shortcut. There are tools to minify your code – Sterling Archer Feb 12 '14 at 22:22
  • I understand that. Wouldn't the first normally minify to something like if(a===b){return true}else{return false}, and the second minify to something like return a===b ? – Josh Beam Feb 12 '14 at 22:24
  • To me, this is a terrible example - the first choice of `if ... else` takes way longer to read, whereas `return foo == bar` is faster to read and understand. – tehftw Sep 25 '18 at 12:45

1 Answers1

8

The two are not mutually exclusive. You should use a well-known minifier like YUI Compressor or Closure.

Write your code to be readable and maintainable. Then optimize it if necessary. A minifier will work well on almost any code. The extra effort to make something "minify smaller" is so small it's really not worth doing.

Codeman
  • 12,157
  • 10
  • 53
  • 91
  • +1 Makes sense, thanks for the response. "Minifying smaller" is a good term for what I meant in my question. – Josh Beam Feb 12 '14 at 22:25
  • Great! Glad I could help. Please make sure to mark the answer as accepted if it answered your question :) – Codeman Feb 12 '14 at 22:25