Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing JavaScript for minification vs. for optimization [closed]

Tags:

javascript

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'.

like image 779
Josh Beam Avatar asked Feb 13 '23 19:02

Josh Beam


1 Answers

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.

like image 198
Codeman Avatar answered Apr 09 '23 22:04

Codeman