admin管理员组

文章数量:1594222

Yesterday Twitter went nuts over a little site called “You Might Not Need jQuery”. The site’s seemingly innocent claim was that many things that you think you need jQuery for are actually not so bad in plain old JavaScript, depending on what browsers you are supporting.

昨天,Twitter在一个名为“您可能不需要jQuery”的小网站上发疯了。 该网站看似无辜的说法是,您认为需要jQuery的许多东西实际上在普通的旧JavaScript中并不是那么糟糕,这取决于您所支持的浏览器。

jQuery and its cousins are great, and by all means use them if it makes it easier to develop your application.

jQuery及其表亲非常出色,如果可以简化应用程序的开发,请务必使用它们。

If you’re developing a library on the other hand, please take a moment to consider if you actually need jQuery as a dependency. Maybe you can include a few lines of utility code, and forgo the requirement.

另一方面,如果您正在开发库,请花一点时间考虑是否确实需要jQuery作为依赖项。 也许您可以包括几行实用程序代码,然后放弃要求。

It then proceeded to provide example code—what a line of jQuery was compared to the vanilla JavaScript alternative. Not all of these snippets were exactly identical to the jQuery code in terms of what they accomplished, but they were pretty close.

然后,它继续提供示例代码-将jQuery的行与普通JavaScript替代项进行了比较。 就它们完成的功能而言,并非所有这些代码片段都与jQuery代码完全相同,但是它们非常接近。

The site itself didn’t seem particularly exciting or controversial. We’ve seen these sorts of comparison before, and frankly claiming that you “might” not need jQuery is an awful innocent statement to make.

该网站本身似乎并不特别令人兴奋或引起争议。 我们之前已经看过这种比较,并且坦率地声称您“可能”不需要jQuery是一个可怕的无辜声明。

But the reaction was more divided than I expected with many getting a little worked up about the site. Which of course causes me to wonder—if suggesting we “might” not need a technology can cause such a heated discussion, perhaps we “might” be just a little too attached to it.

但是React比我预期的要分散得多,许多人对该站点进行了一些处理。 当然,哪一个使我感到怀疑-如果暗示我们“可能”不需要某种技术会引起如此激烈的讨论,也许我们“可能”对此有点依附。

It’s not that I don’t like or use libraries and frameworks. I do. In fact, I use tools like jQuery and Ender quite frequently. A good library brings the benefit of being well-tested, documented (well, in some cases at least) and can be very helpful for complex functionality or when working in a team environment.

不是我不喜欢或使用库和框架。 我做。 实际上,我经常使用jQuery和Ender之类的工具。 一个好的库带来了经过良好测试,记录(至少在某些情况下还可以)的好处,并且对于复杂的功能或在团队环境中工作非常有帮助。

What worries me is that for many, libraries have become the default. They’re rolled into boilerplates and pattern libraries as an assumed dependency. And if we know anything about default settings, it’s that most people will stick with them. This undoubtedly leads to many projects incurring this overhead without every giving consideration to whether it is really necessary.

让我担心的是,对于许多人来说,库已成为默认库。 将它们作为假定的依赖关系卷入样板和模式库。 如果我们对默认设置一无所知,那就是大多数人都会坚持使用它们 。 无疑,这导致许多项目产生了这种开销,而没有全盘考虑是否确实必要。

According to the the latest run of HTTPArchive’s top 1000 sites (January 15, 2014), the average weight for a page is 1463kb. Scripts weigh in at 272kb, second only to images in total weight. Mobile tells a similar story. The average site on a mobile device weighs in at 717kb, of which 168kb is JavaScript.

根据最新的HTTPArchive前1000个站点的运行情况(2014年1月15日),页面的平均权重为1463kb。 脚本的总重为272kb,仅次于图像。 Mobile讲述了一个类似的故事。 移动设备上的平均站点重717kb,其中168kb是JavaScript。

Compare those numbers to the start of the year and we see an alarming trend. Script weight is up 28% from the start of the year on desktop pages and up 22% on mobile.

将这些数字与年初进行比较,我们会看到一个惊人的趋势。 与年初相比,台式机页面的脚本权重增加了28%,移动设备的脚本权重增加了22%。

This is concerning, but it’s not just download sizes that you should be worried about. In a presentation given at Velocity in 2011, Maximiliano Firtman pointed out that on some phones (older, but still popular, BlackBerry devices for example) can take up to 8 seconds just to parse jQuery. More recent research from Stoyan Stefanov revealed that even on iOS 5.1, it was taking as many as 200-300ms to parse jQuery.

这很重要,但您不仅要担心下载大小。 在2011年Velocity上的一次演讲中, Maximiliano Firtman指出 ,在某些手机(例如较旧但仍很流行的BlackBerry设备)上, 解析 jQuery最多可能需要8秒钟。 Stoyan Stefanov的最新研究表明,即使在iOS 5.1上,解析jQuery也要花费200-300ms的时间。

This isn’t even the worst case scenario. I’ve worked on projects where some of the devices we needed to test on couldn’t load the page at all if jQuery was present—it was just too much JavaScript for the device to handle.

这甚至不是最坏的情况。 我参与的项目中,如果存在jQuery,我们需要测试的某些设备根本无法加载页面-对于该设备而言,JavaScript太多了。

Performance is not the only concern. At times, the abstraction that libraries and frameworks provide can actually be harmful. Without an understanding of the underlying language in use, it can confuse developers as much as it aides them.

性能不是唯一的问题。 有时,库和框架提供的抽象实际上可能是有害的 。 如果不了解所使用的基础语言,那么它会给开发人员带来极大的困惑,同时也会给开发人员带来很大的帮助。

I’m know I’m picking on jQuery, but that’s primarily because of its un-paralleled popularity. My concerns with starting with a library as a default method of coding are not confined to any one library in particular.

我知道我选择了jQuery,但这主要是因为它无与伦比的受欢迎程度。 我对以库作为默认编码方法的关注并不特别局限于任何一个库。

It seems this is a tricky topic to approach because it is so often viewed as being black or white: you’re either against frameworks or you’re opposed to them. Like so many topics, people can get religious about this stuff. But it’s not about what is the right™ way to do it: it’s about using the best tool for the job and arming yourself with the knowledge necessary to make that determination.

似乎这是一个棘手的话题,因为它经常被视为黑色或白色:您要么反对框架,要么反对框架。 像许多主题一样,人们可以对此宗教产生兴趣。 但是,这与正确的™方法无关;而在于使用最佳工具完成工作,并为自己提供必要的知识以做出决定。

The reality is that you don’t always need to use a framework or library. Often times, you can get by with just a little bit of native JavaScript, saving precious bytes and seconds while doing so. When the job calls for frameworks, then use a framework (and do so responsibly). When you can do it just as well with vanilla JavaScript, then roll your own. (For anyone thinking about commenting “Don’t worry about it, just include one less image on your site” response, I’m going to preemptively respond: why not do both? One less image is not an excuse for not being careful about JS size.)

现实情况是,您并不总是需要使用框架或库。 通常,您只需要一点点本机JavaScript,就可以节省宝贵的字节和秒数。 当工作需要框架时,请使用框架( 并负责任地使用 )。 当您可以使用普通JavaScript很好地做到这一点时,然后自己动手制作。 (对于任何想评论“不用担心,只在您的网站上少添加一张图片”评论的人,我都会先发制人地做出回应:为什么不同时做这两个呢?少一张图片不是您不小心的借口JS大小。)

Everything has a cost associated with it. Whenever we add something to our sites we need to be able to think critically about whether or not the value outweighs the cost. JavaScript libraries are no exception.

一切都有相关的成本。 每当我们在网站上添加内容时,我们都需要能够认真思考价值是否超过成本。 JavaScript库也不例外。

I’m not saying that we stop using libraries altogether—and neither were the people who created “You Might Not Need jQuery”. I’m suggesting we make that decision with a great deal of care.

我并不是说我们完全停止使用库,而创建“您可能不需要jQuery”的人也没有。 我建议我们在做出这个决定时要格外小心。

翻译自: https://timkadlec/2014/01/smart-defaults-on-libraries-and-frameworks/

本文标签: 默认值框架智能