jq-vs-joy

JavaScript performance comparison

Test case created

Preparation code

<script>
!function(e,t){"use strict";function o(e,t,n,r){this.el=this._init(e),this.isClone=r||s.isHtml(e),this.firstCollect=t,this.prevCollect=n,this.length=this.el.length}function u(e){return new o(e)}var n="$",r=Array.prototype,i=t.JOY={noConflict:function(){return delete t[n],u},typeOf:function(e){return{}.toString.call(e).match(/\s(\w+)/)[1].toLowerCase()},toArray:function(e){return r.slice.call(e)},curry:function(e,t){return function(){return e.apply(t||arguments[0],i.toArray(arguments))}},flatten:function(e){return r.concat.apply([],e)},uniq:function(e){return e.filter(function(t,n){return e.indexOf(t)==n})},removeEmpty:function(e){return e.filter(function(e){return e})},extend:function(e,t){for(var n in t)e[n]=t[n];return e},range:function(e,t,n){var r=(new Array(++t-e)).join(" ").split(" ").map(function(t,n){return e+n});return n?r.map(function(e){return[Math.random(),e]}).sort().map(function(e){return e[1]}):r}},s={isJoy:function(e){return e instanceof o},isHtml:function(e){return/^</.test(e)},isDomEl:function(e){return i.toArray(e).every(function(e){return/^html/.test(i.typeOf(e))})},isHtmlOrDocument:function(e){return/htmlhtml|htmldocument/.test(i.typeOf(e))},dummy:function(){return e.createElement("div")},htmlToDom:function(e){var t=s.dummy();return t.innerHTML=e,t.childNodes},splitClass:function(e){return new RegExp("\\b"+e.split(" ").join("|")+"\\b","g")},ucFirst:function(e){return e.replace(/^\w/,function(e){return e.toUpperCase()})},parseArgsAsObj:function(e){var t={};return t[e[0]]=e[1],e.length==1?e[0]:t},attrToData:function(e){var t=s.parseArgsAsObj(e);if(!t)return;return i.typeOf(t)=="string"?"data-"+t:(Object.keys(t).forEach(function(e){t["data-"+e]=JSON.stringify(t[e]),delete t[e]}),t)},getSpecialPseudo:function(e){var t=(/:(.+)/.exec(e)||[,null])[1],n=!!~Object.keys(i.specialPseudo).indexOf(t);return n&&t},loopAll:function(e,t,n){return!function r(e){var i=e[t];if(!i)return;i.nodeType==1&&!~n.indexOf(i)&&n.push(i),r(i)}(e),n},getEventFromString:function(e){var t=/(.*):(.*)/.exec(e)||[,e,null],n="_joy-event-";return{e:t[1],"native":n+t[1],custom:t[2]&&n+t[1]+"-"+t[2]}},userCapture:function(e){return/focus|blur/.test(e)},findEventsThatMatch:function(e,t){var n=s.getEventFromString(t),r=n.custom in e;return r?[n.custom]:Object.keys(e).filter(function(e){return(new RegExp(n.native)).test(e)})},transferProps:function(e,t){return Object.keys(e).forEach(function(n){/_joy-/.test(n)&&(t[n]=e[n]);var r=(/_joy-event-(.+)/.exec(n)||[,null])[1];r&&t.addEventListener(r,t["_joy-event-"+r])}),t},generateMethods:function(e){e.forEach(function(e){var t=e[0].split(" "),n=e[1];t.forEach(function(e){o.prototype[e]=function(){var t=i.flatten([this,e,i.toArray(arguments)]);return n.apply(this,t)}})})}};i.extend(i,{specialPseudo:{hidden:function(){var e=u(this);return e.css("display")=="none"||e.css("visibility")=="hidden"},visible:function(){return!u(this).is(":hidden")},checked:function(){return this.checked},unchecked:function(){return!this.checked},first:function(e){return e.first().el[0]==this},last:function(e){return e.last().el[0]==this},odd:function(e){return!!~e.odd().el.indexOf(this)},even:function(e){return!!~e.even().el.indexOf(this)}}}),i.extend(o.prototype,{_new:function(e,t){var n=i.extend({firstCollect:this.first||this.el,prevCollect:this.el,isClone:!1},t||{});return new o(e,n.firstCollect,n.prevCollect,n.isClone)},_init:function(t){return t?s.isJoy(t)?t.el:s.isHtml(t)?i.toArray(s.htmlToDom(t)):s.isDomEl(t)?r.concat.call(t):i.typeOf(t)=="array"?t:i.toArray(e.querySelectorAll("html "+t)):[]},_withElements:function(e,t){var n=[];return this.each(function(t,r){~n.indexOf(this)||e.call(this,n,t,r)}),this._new(i.uniq(i.flatten(n)))._matches(t)},_withTargets:function(e,t){var n=this,r=this._new(e);return this._withElements(function(e,i){r.each(function(r,s){var o=n._new(i).clone(!0).el[0],u=n.isClone?o:i;t.call(u,r,s),e.push(u)})})},_loopAll:function(e,t){return e=e.split(" "),this._withElements(function(t){e.forEach(function(e){s.loopAll(this,e,t)}.bind(this))})._matches(t)},_matches:function(e){var t=this;return e?this._withElements(function(n,r){var o,u=i.specialPseudo[s.getSpecialPseudo(e)];typeof e=="string"&&(u?(o=u.call(this,t),o&&n.push(this)):(o=this.parentNode.querySelectorAll(e),i.toArray(o).forEach(function(e){e==r&&n.push(r)}))),s.isJoy(e)&&e.each(function(){this==r&&n.push(r)})}):this},_query:function(t){var n=this;return this._withElements(function(r){var o=n._new(this),u=i.toArray(this.children),a;t||r.push(u),n.isClone&&r.push(i.toArray(this.querySelectorAll(t))),s.isJoy(t)&&r.push(n._new(u)._matches(t).el);if(typeof t=="string"){if(o.hasClass("_joy-uniq"))return;o.addClass("_joy-uniq"),a=e.querySelectorAll("._joy-uniq "+t.split(",").join(",._joy-uniq ")),r.push(i.toArray(a)),o.removeClass("_joy-uniq")}})}}),i.extend(o.prototype,{each:function(e){return this.el.some(i.curry(e)),this},map:function(e){return this._new(this.el.map(i.curry(e)))},slice:function(e,t){return this._new(this.el.slice(e,t))},find:function(e){return this._query(e)},children:function(e){var t=this;return this._withElements(function(e){var n=t._new(this).find().el;n&&e.push(n)},e)},end:function(){return this._new(this.first)},add:function(e){return this._new(this.el.concat(this._new(e).el))},addBack:function(){return this.add(this.prevCollect)},filter:function(e){return e?this._matches(e):this},eq:function(e){return this._new(this.el[e])},is:function(e){return this._matches(e).length},has:function(e){var t=this;return this._withElements(function(n){t._new(this).find("*").filter(e).length&&n.push(this)})},not:function(e){var t=this;return this._withElements(function(n){var r=t._new(this)._matches(e).el[0];this!=r&&n.push(this)})},first:function(){return this._new(this.el[0])},last:function(){return this._new(this.el[this.length-1])},parents:function(e){return this._loopAll("parentNode",e)},parent:function(e){var t=this;return this._withElements(function(e){e.push(t._new(this).parents().el[0])},e)},closest:function(e){var t=this;return this._withElements(function(n){n.push(t._new(this).parents(e).el[0])})},siblings:function(e){return this._loopAll("previousSibling nextSibling",e)},prevAll:function(e){return this._loopAll("previousSibling",e)},nextAll:function(e){return this._loopAll("nextSibling",e)},index:function(e){var t=this.parent().find().el,n=this.filter(e).el[0];return e?this.el.indexOf(n):t.indexOf(n)}}),i.extend(o.prototype,{clone:function(e){var t=[];return this.each(function(){var n=this.cloneNode(!0);t.push(e?s.transferProps(this,n):n)}),this._new(t,{isClone:!0})},remove:function(){return this.each(function(){this.parentNode.removeChild(this)})},empty:function(){return this.each(function(){while(this.hasChildNodes())this.removeChild(this.lastChild)})},appendTo:function(e){return this._withTargets(e,function(e){e.appendChild(this)})},prependTo:function(e){return this._withTargets(e,function(e){e.insertBefore(this,e.firstChild)})},insertAfter:function(e){return this._withTargets(e,function(e){e.parentNode.insertBefore(this,e.nextSibling)})},insertBefore:function(e){return this._withTargets(e,function(e){e.parentNode.insertBefore(this,e)})},wrapAll:function(e){return this.appendTo(this._new(e).appendTo(this.parent()))},wrap:function(e){var t=this,n=i.typeOf(e)=="function";return this.each(function(r,i){t._new(this).wrapAll(n?e.call(this,r,i):e)})},replaceWith:function(e){var t=this;return this.each(function(){t._new(this).after(e).remove()})}}),i.extend(o.prototype,{css:function(){var e=s.parseArgsAsObj(arguments);return typeof e=="string"?t.getComputedStyle(this.el[0])[e]:this.each(function(){for(var t in e)this.style[t]=e[t]})},val:function(e){return e?this.each(function(){this.value=e}):this.el[0].value},attr:function(){var e;if(arguments.length==1&&typeof arguments[0]=="string"){if(!s.isHtmlOrDocument(this.el[0])){e=this.el[0].getAttribute(arguments[0]);try{return JSON.parse(e)}catch(t){return e}}return}return e=s.parseArgsAsObj(arguments),this.each(function(){for(var t in e)s.isHtmlOrDocument(this)||this.setAttribute(t,e[t])})},removeAttr:function(){var e=i.toArray(arguments);return this.each(function(){e.forEach(function(e){this.removeAttribute(e)}.bind(this))})},data:function(){return this.attr(s.attrToData(arguments))},removeData:function(){return this.removeAttr(s.attrToData(arguments))}}),i.extend(o.prototype,{hasClass:function(e){return s.splitClass(e).test(this.el[0].className)},addClass:function(e){return this.each(function(){this.className=(this.className+" "+e.split(" ").join(" ")).trim()})},removeClass:function(e){return this.each(function(){var t=s.splitClass(e);this.className=this.className.replace(t,"").trim()})},toggleClass:function(e){var t=this;return this.each(function(n){var r=e.split(" ");r.forEach(function(e){var r=(new RegExp(e)).test(n.className),i=r?"remove":"add";t._new(n)[i+"Class"](e)})})},hide:function(){var e=this;return this.each(function(){e._new(this).css("display","none")})},show:function(){var e=this;return this.each(function(){e._new(this).css("display","")})},toggle:function(){var e=this;return this.each(function(){var t=e._new(this);t[t.css("display")=="none"?"show":"hide"]()})}}),i.extend(o.prototype,{on:function(e,t){return e=s.getEventFromString(e),this.each(function(){this[e.custom||e.native]=t,this.addEventListener(e.e,t,s.userCapture(e.e))})},off:function(e){var t=s.getEventFromString(e);return this.each(function(){s.findEventsThatMatch(this,e).forEach(function(e){this.removeEventListener(t.e,this[e]),delete this[e]}.bind(this))})},one:function(e,t){var n=this;return this.on(e,function(r,i){t.call(this,r,i),n._new(this).off(e)})},delegate:function(e,t,n){var r=this;return this.on(e,function(e){var i=~r._new(this).find().filter(t).el.indexOf(e.target);i&&n.call(e.target,e)})},trigger:function(e){return this.each(function(){s.findEventsThatMatch(this,e).forEach(function(e){this[e]()}.bind(this))})}}),i.extend(o.prototype,{test:function(e){return this._withElements(function(t){e&&e.test(this.textContent)&&t.push(this)})},queue:function(e,n){return this.each(function(r,s){t.setTimeout(i.curry(e,this),(n||400)*s)})},sort:function(e){var t=this;return this.each(function(){var n=t._new(this).find().el;return t._new(n.sort(e?i.curry(e):function(e,t){return e.textContent>t.textContent?1:-1})).appendTo(t)})},shuffle:function(){var e=this;return this.each(function(){var t=e._new(this),n=t.find().el,r=i.range(0,n.length-1,!0).map(function(e){return n[e]});n.length&&e._new(r).appendTo(t)})}}),s.generateMethods([["odd even",function(e,t,n){return this.filter(":nth-child("+t+")").filter(n)}],["prev next",function(e,t,n){return this._withElements(function(r){r.push(e._new(this)[t+"All"](n).eq(0).el)})}],["append prepend",function(e,t,n){return this.each(function(){e._new(n)[t+"To"](this)})}],["before after",function(e,t,n){return this.each(function(){e._new(n)["insert"+s.ucFirst(t)](this)})}],["text html",function(e,t,n){var r=i.typeOf(n)=="function",s=t=="text"?"textContent":"innerHTML";return n?this.each(function(e,t){this[s]=r?n.call(this,e,t):n}):this.el.map(function(e){return e[s]}).join("")}],["width height",function(e,n,r){return this.el[0]==t?t[n=="width"?"innerWidth":"innerHeight"]:r?this.each(function(){e._new(this).css(n,r+"px")}):this.eq(0).css(n).replace("px","")}],["prevUntil nextUntil parentsUntil",function(e,t,n){var r=/(.+)U/.exec(t)[1],i=r=="parents";return this._withElements(function(t){var s=e._new(this)[r+(i?"":"All")](),o=s.filter(n).el[0];t.push(s.slice(0,s.el.indexOf(o)).el)})}],["load unload error resize scroll click dblclick focus blur focusin focusout mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave change select keydown keypress keyup",function(e,t,n){return n?this.on(t,n):this.trigger(t)}]]),t[n]=u}(document,window);
</script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<div><p><em><i></i></em></p></div>
<script>
Benchmark.prototype.setup = function() {
    var j = jQuery.noConflict();
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
jq
j('i').parents()
pending…
joy
$('i').parents()
pending…

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

Compare results of other browsers

0 comments

Add a comment