Home > Article > Web Front-end > Some tips for writing efficient JSLite code imitating JQuery_jquery
It is not uncommon for articles to discuss jQuery and javascript performance. However, I plan to teach you how to improve your JSLite and JavaScript code based on some speed tips and suggestions that others have put together for jQuery. Good code will bring speed improvements. Fast rendering and responsiveness means a better user experience.
First of all, keep in mind that JSLite is javascript. This means we should adopt the same coding conventions, style guides and best practices.
First of all, if you are a javascript newbie and have never used jQuery, I suggest you read the syntax introduction of the official document first. This is a high-quality javascript tutorial, which means that you have been using jQuery for a while. .
When you are ready to use JSLite, I strongly recommend that you follow these guidelines:
Cache variables
DOM traversal is expensive, so try to cache reused elements.
JSLite is the same as JavaScript. Generally speaking, it is best to ensure that your variables are within function scope.
Add $ prefix before variables to easily identify JSLite objects.
Consolidate multiple var statements into one statement. I recommend putting unassigned variables at the end.
Please use on
In the new version of JSLite, the shorter on("click") is used to replace functions like click(). In previous versions on() was bind() . on() is the preferred method of attaching event handlers. However, for consistency's sake, you can simply use the on() method all together.
$first.hover(function(){
$first.css('border','1px solid red');
})
// Suggestion
$first.on('click',function(){
$first.css('border','1px solid red');
$first.css('color','blue');
})
$first.on('hover',function(){
$first.css('border','1px solid red');
})
In general, it’s best to combine functions whenever possible.
It is very easy to implement chained operations of methods in JSLite. Take advantage of this below.
Maintain code readability
Along with streamlining the code and using chaining, the code may become difficult to read. Adding pinches and line breaks can work wonders.
Short-circuit evaluation is an expression that is evaluated from left to right, using the && (logical AND) or || (logical OR) operators.
One way to streamline your code is to take advantage of coding shortcuts.
If you plan to do a lot of operations on DOM elements (setting multiple attributes or css styles in succession), it is recommended to separate the elements first and then add them.
$element = $containerLi.first();
//... Many complex operations
// better
var
$container = $("#container"),
$containerLi = $container.find("li"),
$element = null;
$element = $containerLi.first().detach();
//... Many complex operations
$container.append($element);
You may be inexperienced with using methods in JSLite, be sure to check out the documentation, there may be a better or faster way to use it.
As mentioned earlier, DOM traversal is an expensive operation. A typical approach is to cache parent elements and reuse these cached elements when selecting child elements.
Putting the universal selector into a descendant selector has terrible performance.
Universal selectors are sometimes implicit and difficult to find.
For example, the Id selector should be unique, so there is no need to add additional selectors.
I emphasize here that the ID selector should be unique, there is no need to add additional selectors, and there is no need for multiple descendant ID selectors.
New versions are usually better: more lightweight, more efficient, with more methods, and more comprehensive coverage of jQuery methods. Obviously, you need to consider the compatibility of the code you want to support. For example, does the project run on good support for HTML5/CSS3
Combine JSLite and javascript native code when necessary
As mentioned above, JSLite is javascript, which means that anything you can do with JSLite can also be done with native code. Native code may not be as readable and maintainable as JSLite, and the code may be longer. But it also means more efficient (usually the closer to the underlying code the less readable the higher the performance, for example: assembly, which of course requires more powerful people). Keep in mind that no framework can be smaller, lighter, and more efficient than native code (note: the test link is no longer valid, you can search for the test code online).
Final advice
Finally, I record this article with the purpose of improving the performance of JSLite and some other good suggestions. If you want to delve deeper into this topic you will find a lot of fun. Remember, JSLite is not required, just an option. Think about why you want to use it. DOM manipulation? ajax? stencil? css animation? Or a selector? Heavy jQuery developer?