简体   繁体   English

jQuery / Javascript框架效率

[英]jQuery/Javascript framework efficiency

My latest project is using a javascript framework (jQuery), along with some plugins (validation, jquery-ui, datepicker, facebox, ...) to help make a modern web application. 我最新的项目是使用javascript框架(jQuery),以及一些插件(验证,jquery-ui,datepicker,facebox等)来帮助创建现代Web应用程序。

I am now finding pages loading slower than I am used to. 我现在发现页面加载速度比以前慢。 After some js profiling (thanks VS2010!), it seems a lot of the time is taken processing inside the framework. 经过一些js分析(感谢VS2010!),似乎很多时间都花在了框架内部的处理上。

Now I understand the more complex the ui tools, the more processing needs to be done. 现在,我了解ui工具越复杂,需要完成的处理越多。 The project is not yet at a large stage and I think would be average functions. 该项目尚未进入大型阶段,我认为这将是平均职能。 At this stage I can see it is not going to scale well. 在这个阶段,我可以看到它的伸缩性不好。

I noticed things like the 'each' command in jQuery takes quite a lot of processing time. 我注意到诸如jQuery中的“ each”命令之类的事情需要花费大量的处理时间。

Have others experienced some extra latency using JS frameworks? 其他人是否使用JS框架经历了一些额外的延迟? How do I minimize their effect on page performance? 如何最小化它们对页面性能的影响? Are there best practices on implementation using JS frameworks? 是否有使用JS框架实施的最佳实践?

Thanks 谢谢

My personal take is to use the framework methods and tools where they make sense and make life easier, for example selectors and solving cross-browser quirks, and to use plain old vanilla JavaScript where there is no need to use the framework methods, for example, in simple loops. 我个人的看法是使用有意义且使生活更轻松的框架方法和工具,例如选择器和解决跨浏览器的怪癖,并在不需要使用框架方法的地方使用普通的原始JavaScript,例如,在简单的循环中。

I would check and double check the code that you have that uses the framework to ensure that it will perform as well as it can; 我将检查并仔细检查您使用该框架的代码,以确保其性能良好。 it is all too easy to use a framework in a poor performing fashion and sometimes one doesn't discover this until one profiles it :) 以性能低下的方式使用框架太容易了,有时只有在对其进行概要分析后才发现它:)

Frameworks do introduce extra latency as there are usually a number of functions that are executed as a result of using the entry point function into using them. 框架确实引入了额外的延迟,因为通常使用入口点函数来执行它们会导致执行许多功能。

EDIT: 编辑:

Some general points with regards to using jQuery: 关于使用jQuery的一些一般要点:

1.cache your jQuery objects in local variables if you are going to use them more than once. 1.如果要多次使用jQuery对象,请在局部变量中缓存它们。 Querying the DOM is a relatively expensive operation and therefore should be done as little as is needed. 查询DOM是一项相对昂贵的操作,因此应尽可能少地执行。 If you have related selectors, take a look at performing a wide selection and then using the methods such as find() , filter() next() , prev() etc to filter the collection to get the relevant elements that you would have usedanother selector function to get. 如果您有相关的选择器,请看一下执行广泛的选择,然后使用诸如find()filter() next()prev()等方法对集合进行过滤,以获取您本来会使用的相关元素选择器函数获取。

2.Inside of functions, don't wrap objects in jQuery objects unneccessarily. 2.在函数内部,不要不必要地将对象包装在jQuery对象中。 If there is a cross browser way of accessing an object property value that is reliable, then use that. 如果存在跨浏览器的方法来访问可靠的对象属性值,请使用该方法。 For example, the value property of a text input HTMLElement 例如,文本输入HTMLElement的value属性

$('input:text').each(function() {
    // use
    this.value

    // don't worry about this
    $(this).val();
 }); 

3.Try to avoid adding large script files where you're using only a small piece of the functionality. 3.尽量避免在只使用一小部分功能的情况下添加大脚本文件。 There can be a lot of time spent parsing and executing code on page load that you're never going to use! 在页面加载时,可能有很多时间花在解析和执行代码上,这些都是您永远不会使用的! If possible, extract only the relevant code that is needed. 如果可能,仅提取所需的相关代码。 I appreciate that this can be hard and is not always possible, particularly when it comes to versioning, but it's worth pointing out nonetheless. 我知道这可能很难并且并非总是可能的,特别是在版本控制方面,但是还是值得指出的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM