为缩小和优化编写JavaScript [关闭](Writing JavaScript for minification vs. for optimization [closed])

编程入门 行业动态 更新时间:2024-10-26 12:31:39
为缩小和优化编写JavaScript [关闭](Writing JavaScript for minification vs. for optimization [closed])

通过优化,我的意思是例如在使用基本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 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'.

最满意答案

这两者不是相互排斥的。 你应该使用像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.

更多推荐

本文发布于:2023-08-04 16:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417731.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Writing   JavaScript   minification   closed   optimization

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!