通过优化,我的意思是例如在使用基本for循环与Array.prototype.forEach()之间进行决定,基于哪个实现似乎在哪些浏览器等方面更快(并且在某些情况下,这可以扩展到微优化他们说这可能很危险 。)
以下是'为缩小而写'可能意味着什么的一个例子:
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有些人可能认为使用if else语句乍一看有点可读,但我会说第二种写入方式更好。
就缩小而言,从我的理解来看,其最大的目的是减少服务器响应时间。
编写minifcation和服务器响应时间与可读性之间的平衡是什么,与写入“优化”?
编辑 有人可能会将这个问题改写为写优化与写作,以使代码“缩小” 。
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 toSome 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'.
最满意答案
这两者不是相互排斥的。 你应该使用像YUI Compressor或Closure这样众所周知的缩放器。
编写代码以使其易于阅读和维护。 然后在必要时进行优化。 缩小器几乎可以在任何代码上运行良好。 制作“缩小”的额外努力是如此之小,以至于真的不值得做。
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.
更多推荐
发布评论