这里声明一下,这不是反jQuery的文章,jQuery做为一个js库给你们的项目开发带来不少便利,但有时候仔细想一想,咱们真的须要jQuery吗?一年前的lpisme的主题的一个特点是没有jQuery,仍是如今的Pinghsu主题,也是不用jQuery的。这里我想告诉你们,我持有的观点是在中小型的项目中建议能不用jQuery就不用。javascript
在全部的现代浏览器(IE9+)里,它们所提供的原生DOM API都是比jQuery快不少。为何?html
有一个东西,叫Vanilla JS,它是一个快速、轻量级、跨平台的JavaScript框架。几乎全部著名的互联网企业都使用它。java
同时,它也是这个世界上最轻量级的javascript框架(没有之一),它有多快? 以下node
咱们在HTML里引入Vanilla JS:jquery
<script src="path/to/vanilla.js"></script>
比上面更快的方法是:git
・
什么?没有代码?是的,就是没有代码,由于Vanilla JS实在太强了,以致于全部的浏览器在10年前内置了它。github
因此,咱们平时吹牛逼说的什么原生js的实现,用到什么原生API,都是来自于Vanilla JSajax
在这里,咱们用原生API和各类库进行性能对比,数据来源请看参考浏览器
框架 | 代码 | 次数/秒 |
---|---|---|
Vanilla JS | document.getElementById('test-table'); | 12,137,211 |
Dojo | dojo.byId('test-table'); | 5,443,343 |
Prototype JS | $('test-table') | 2,940,734 |
Ext JS | delete Ext.elCache['test-table'];Ext.get('test-table'); | 997,562 |
jQuery | $jq('#test-table'); | 350,557 |
YUI | YAHOO.util.Dom.get('test-table'); | 326,534 |
MooTools | document.id('test-table'); | 78,802 |
框架 | 代码 | 次数/秒 |
---|---|---|
Vanilla JS | document.getElementsByTagName("span"); | 8,280,893 |
Prototype JS | Prototype.Selector.select('span', document); | 62,872 |
YUI | YAHOO.util.Dom.getElementsBy(function(){return true;},'span'); | 48,545 |
Ext JS | Ext.query('span'); | 46,915 |
jQuery | $jq('span'); | 19,449 |
Dojo | dojo.query('span'); | 10,335 |
MooTools | Slick.search(document, 'span', new Elements); | 5,457 |
Done,Vanilla JS all win~框架
下面是一些经常使用的jQuery方法,以及它们在原生JavaScript中的对应方法。
// jQuery $(document).ready(readyCb); or $(readyCb); // VanillaJs function docReady(cb) { if (document.readyState != 'loading'){ cb(); } else { document.addEventListener('DOMContentLoaded', cb); } } docReady(readyCb);
更多Selector的性能表现请看这里:here
// jQuery const items = $('.item'); // VanillaJS const items = document.getElementsByClassName('item');
// jQuery const item = $('#item'); // VanillaJS const item = document.getElementById('item');
// jQuery const items = $('.list .item'); const lastItem = $('.item:last-item'); // VanillaJS const items = document.querySelectorAll('.list .item'); const lastItem = document.querySelector('.item:last-item');
// jQuery $('.item').each(function(index, element) { console.log(element); }); // VanillaJS function each(nodeList, cb) { for(var i = 0; i < nodeList.length;i++) { cb(nodeList[i], i, nodeList); } } each(document.getElementsByClassName('item'), function(node, i) { console.log(node); }); // Another Vanilla forEach Array.prototype.forEach.call(document.querySelectorAll('.item'), function(node, i){ console.log(node); });
// jQuery const item = $('#item') item.addClass('new-class'); item.removeClass('new-class'); // VanillaJS const item = document.getElementById('item'); item.classList.add('new-class'); item.classList.remove('new-class');
// jQuery const item = $('#item'); item.hide(); item.show(); // VanillaJS const item = document.getElementById('item'); item.style.display = 'none'; item.style.display = '';
代替$.ajax
你有下面几种方法
const xhr = new XMLHttpRequest(); xhr.addEventListener("load", function() { // response can be used here }); xhr.open('GET', 'url'); xhr.send();
大多数的主流浏览器都支持Fetch方法,你能够用 polyfills 让更多浏览器支持
你也能够在 CanIUse 里能够查看更多浏览器支持状况
fetch(’url’) .then(function (response) {}) .catch(function (error) {});
若是你须要查看更多例子,能够访问here
在浏览器野蛮生长的年代,jQuery做为一种工具在当时几乎是必需的。但随着浏览器们愈来愈标准化,浏览器之间的API差异也在减小,而且经过版本迭代也会更快地支持,咱们能够更好地用原生API作更高效的事。这里不是说jQuery很差,只是咱们作项目的时候,不该该把它做为默认。咱们都有Vanilla JS了,已是火箭炮了,还要啥自行车呢?
谢谢你们阅读,欢迎访问个人博客:https://www.linpx.com/