cash-vs-shshaw-vs-jquery (v7)

Revision 7 of this benchmark created by Shaw on


Preparation HTML

<div id="id-test"></div>
<div class="class-test"></div>
<ul>
<li>Element Test</li>
</ul>

<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/cash/1.0.0/cash.min.js"></script>

<script>
// shshaw's cash
"use strict";!function(t,n){"function"==typeof define&&define.amd?define(n):"undefined"!=typeof exports?module.exports=n():t.shshaw=t.$=n()}(this,function(){function t(t,n){n=n||C;var e=O.test(t)?n.getElementsByClassName(t.slice(1)):R.test(t)?n.getElementsByTagName(t):n.querySelectorAll(t);return e}function n(t){return x=x||C.createDocumentFragment(),L=L||x.appendChild(C.createElement("div")),L.innerHTML=t,L.childNodes}function e(t){"loading"!==C.readyState?t():C.addEventListener("DOMContentLoaded",t)}function r(r,i){if(!r)return this;if(r.cash)return r;var u,o=r,s=0;if(M(r)){if(B.test(r))return this[0]=C.getElementById(r.slice(1)),this.length=this[0]?1:0,this;o=H.test(r)?n(r):t(r,i)}else if(S(r))return e(r),this;if(!o)return this;if(o.nodeType||o===N)this[0]=o,this.length=1;else for(u=this.length=o.length;u>s;s++)this[s]=o[s];return this}function i(t,n){return new r(t,n)}function u(t,n){for(var e=t.length,r=0;e>r&&n.call(t[r],t[r],r,t)!==!1;r++);}function o(t,n){return(t.matches||t.webkitMatchesSelector||t.mozMatchesSelector||t.msMatchesSelector||t.oMatchesSelector).call(t,n)}function s(t){return i(E.call(t).filter(function(t,n,e){return e.indexOf(t)===n}))}function c(t,n){return t.classList?t.classList.contains(n):new RegExp("(^| )"+n+"( |$)","gi").test(t.className)}function a(t,n,e){t.classList?t.classList.add(n):e.indexOf(" "+n+" ")&&(t.className+=" "+n)}function f(t,n){t.classList?t.classList.remove(n):t.className=t.className.replace(n,"")}function h(t,n){return parseInt(N.getComputedStyle(t[0],null)[n],10)||0}function l(){function t(t){var n=(Math.random().toString(16)+"000000000").substr(2,8);return t?"-"+n.substr(0,4)+"-"+n.substr(4,4):n}return t()+t(!0)+t(!0)+t()}function d(t,n,e){var r=i(t).data(I)||l();i(t).data(I,r),r in F||(F[r]={}),n in F[r]||(F[r][n]=[]),F[r][n].push(e)}function p(t,n){return"&"+encodeURIComponent(t)+"="+encodeURIComponent(n).replace(/%20/g,"+")}function v(t){return"radio"===t.type||"checkbox"===t.type}function m(t,n,e){if(e){var r=t.childNodes[0];t.insertBefore(n,r)}else t.appendChild(n)}function g(t,n,e){var r=M(n);return!r&&n.length?void u(n,function(n){return g(t,n,e)}):void u(t,r?function(t){return t.insertAdjacentHTML(e?"afterbegin":"beforeend",n)}:function(t,r){return m(t,0===r?n:n.cloneNode(!0),e)})}function y(t,n){return t===n}var x,L,C=document,N=window,b=Array.prototype,E=b.slice,w=b.filter,A=b.push,T=function(){},S=function(t){return typeof t==typeof T},M=function(t){return"string"==typeof t},B=/^#[\w-]*$/,O=/^\.[\w-]*$/,H=/<.+>/,R=/^\w+$/,q=i.fn=i.prototype=r.prototype={cash:!0,length:0,push:A,splice:b.splice,map:b.map,init:r};i.parseHTML=n,i.extend=q.extend=function(t){t=t||{};var n=E.call(arguments),e=n.length,r=1;for(1===n.length&&(t=this,r=0);e>r;r++)if(n[r])for(var i in n[r])n[r].hasOwnProperty(i)&&(t[i]=n[r][i]);return t},i.extend({merge:function(t,n){for(var e=+n.length,r=t.length,i=0;e>i;r++,i++)t[r]=n[i];return t.length=r,t},each:u,matches:o,unique:s,noop:T,isFunction:S,isString:M,isArray:Array.isArray,isNumeric:function(t){return!isNaN(parseFloat(t))&&isFinite(t)}});var $=/\S+/g;q.extend({addClass:function(t){var n=t.match($);return this.each(function(t){var e=" "+t.className+" ";u(n,function(n){a(t,n,e)})})},attr:function(t,n){return n?this.each(function(e){return e.setAttribute(t,n)}):this[0].getAttribute(t)},hasClass:function(t){var n=!1;return this.each(function(e){return n=c(e,t),!n}),n},prop:function(t){return this[0][t]},removeAttr:function(t){return this.each(function(n){return n.removeAttribute(t)})},removeClass:function(t){var n=t.match($);return this.each(function(t){u(n,function(n){f(t,n)})})},toggleClass:function(t,n){if(void 0!==n)return this[n?"addClass":"removeClass"](t);var e=t.match($);return this.each(function(t){var n=" "+t.className+" ";u(e,function(e){c(t,e)?f(t,e):a(t,e,n)})})}}),q.extend({add:function(t,n){return s(i.merge(this,i(t,n)))},each:function(t){return u(this,t),this},eq:function(t){return i(this.get(t))},filter:function(t){return w.call(this,M(t)?function(n){return o(n,t)}:t)},first:function(){return this.eq(0)},get:function(t){return void 0===t?E.call(this):0>t?this[t+this.length]:this[t]},index:function(t){var n=this[0];return E.call(t?i(t):i(n).parent().children()).indexOf(n)},last:function(){return this.eq(-1)}});var k=function(){function t(t){return t.replace(i,function(t,n){return t[0===n?"toLowerCase":"toUpperCase"]()}).replace(o,"")}var n={},e=C.createElement("div"),r=e.style,i=/(?:^\w|[A-Z]|\b\w)/g,o=/\s+/g;return function(e){if(e=t(e),n[e])return n[e];var i=e.charAt(0).toUpperCase()+e.slice(1),o=["webkit","moz","ms","o"],s=(e+" "+o.join(i+" ")+i).split(" ");return u(s,function(t){return t in r?(n[t]=e=n[e]=t,!1):void 0}),n[e]}}();q.extend({css:function(t,n){if(M(t))return t=k(t),n?this.each(function(e){return e.style[t]=n}):N.getComputedStyle(this[0])[t];for(var e in t)t.hasOwnProperty(e)&&this.css(e,t[e]);return this}}),q.extend({data:function(t,n){return n?this.each(function(e){e.dataset?e.dataset[t]=n:i(e).attr("data-"+t,n)}):this[0].dataset?this[0].dataset[t]:i(this[0]).attr("data-"+t)},removeData:function(t){return this.each(function(n){n.dataset?delete n.dataset[t]:i(n).removeAttr("data-"+t)})}}),u(["Width","Height"],function(t,n){var e=t.toLowerCase();q.extend(function(r){return r[e]=function(){return this[0].getBoundingClientRect()[e]},r["inner"+t]=function(){return this[0]["client"+t]},r["outer"+t]=function(e){return this[0]["offset"+t]+(e?h(this,"margin"+(0===n?"Left":"Top"))+h(this,"margin"+(0===n?"Right":"Bottom")):0)},r}({}))});var F={},I="cshid";q.extend({off:function(t,n){return this.each(function(e){if(n)e.removeEventListener(t,n);else for(var r in F[i(e).data(I)][t])e.removeEventListener(t,F[i(e).data(I)][t][r])})},on:function(t,n,e){return S(n)?(e=n,this.each(function(n){d(i(n),t,e),n.addEventListener(t,e)})):this.each(function(r){function u(t){var i=t.target;if(o(i,n))e.call(i);else{for(;!o(i,n);){if(i===r)return i=!1;i=i.parentNode}i&&e.call(i)}}d(i(r),t,u),r.addEventListener(t,u)})},ready:function(t){e(t)},trigger:function(t){var n=C.createEvent("HTMLEvents");return n.initEvent(t,!0,!1),this.each(function(t){return t.dispatchEvent(n)})}});var P=["file","reset","submit","button"];q.extend({serialize:function(){var t=this[0].elements,n="";return u(t,function(t){t.name&&P.indexOf(t.type)<0&&("select-multiple"===t.type?u(t.options,function(e){e.selected&&(n+=p(t.name,e.value))}):(!v(t)||v(t)&&t.checked)&&(n+=p(t.name,t.value)))}),n.substr(1)},val:function(t){return void 0===t?this[0].value:this.each(function(n){return n.value=t})}}),q.extend({after:function(t){return i(t).insertAfter(this),this},append:function(t){return g(this,t),this},appendTo:function(t){return g(i(t),this),this},before:function(t){return i(t).insertBefore(this),this},clone:function(){return i(this.map(function(t){return t.cloneNode(!0)}))},empty:function(){return this.html(""),this},html:function(t){if(void 0===t)return this[0].innerHTML;var n=t.nodeType?t[0].outerHTML:t;return this.each(function(t){return t.innerHTML=n})},insertAfter:function(t){var n=this;return i(t).each(function(t,e){var r=t.parentNode,i=t.nextSibling;n.each(function(t){r.insertBefore(0===e?t:t.cloneNode(!0),i)})}),this},insertBefore:function(t){var n=this;return i(t).each(function(t,e){var r=t.parentNode;n.each(function(n){r.insertBefore(0===e?n:n.cloneNode(!0),t)})}),this},prepend:function(t){return g(this,t,!0),this},prependTo:function(t){return g(i(t),this,!0),this},remove:function(){return this.each(function(t){return t.parentNode.removeChild(t)})},text:function(t){return t?this.each(function(n){return n.textContent=t}):this[0].textContent}});var U=C.documentElement;return q.extend({position:function(){var t=this[0];return{left:t.offsetLeft,top:t.offsetTop}},offset:function(){var t=this[0].getBoundingClientRect();return{top:t.top+N.pageYOffset-U.clientTop,left:t.left+N.pageXOffset-U.clientLeft}},offsetParent:function(){return i(this[0].offsetParent)}}),q.extend({children:function(t){var n=[];return this.each(function(t){A.apply(n,t.children)}),n=s(n),t?n.filter(function(n){return o(n,t)}):n},closest:function(t){return!t||o(this[0],t)?this:this.parent().closest(t)},is:function(t){if(!t)return!1;var n=!1,e=M(t)?o:t.cash?function(n){return t.is(n)}:y;return this.each(function(r,i){return n=e(r,t,i),!n}),n},find:function(n){if(!n)return i();var e=[];return this.each(function(r){A.apply(e,t(n,r))}),s(e)},has:function(t){return w.call(this,function(n){return 0!==i(n).find(t).length})},next:function(){return i(this[0].nextElementSibling)},not:function(t){return w.call(this,function(n){return!o(n,t)})},parent:function(){var t=this.map(function(t){return t.parentElement||C.body.parentNode});return s(t)},parents:function(t){var n,e=[];return this.each(function(r){for(n=r;n!==C.body.parentNode;)n=n.parentElement,(!t||t&&o(n,t))&&e.push(n)}),s(e)},prev:function(){return i(this[0].previousElementSibling)},siblings:function(){var t=this.parent().children(),n=this[0];return w.call(t,function(t){return t!==n})}}),i});
</script>

Setup

var c = cash;
    var s = shshaw;
    var j = jQuery;
    
    function selectId($){
      return $('#id-test');
    }
    
    function selectClass($){
      return $('.class-test');
    }
    
    function selectTagName($){
      return $('li');
    }
    
    function makeHTML($){
      return $('<div id="me"></div>');
    }

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery ID
selectId(j);
ready
cash ID
selectId(c);
ready
shshaw ID
selectId(s);
ready
jQuery Class
selectClass(j);
ready
cash Class
selectClass(c);
ready
shshaw Class
selectClass(s);
ready
jQuery TagName
selectTagName(j);
ready
cash TagName
selectTagName(c);
ready
shshaw TagName
selectTagName(s);
ready
jQuery Make HTML
makeHTML(j);
ready
cash Make HTML
makeHTML(c);
ready
shshaw Make HTML
makeHTML(s);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.