Test name | Executions per second |
---|---|
jQuery.once (div=10, once=1) | 74483.4 Ops/sec |
once (div=10, once=1) | 199400.2 Ops/sec |
<script src='https://unpkg.com/jquery@3.5.1/dist/jquery.min.js'></script>
<script src='https://unpkg.com/jquery-once@2.2.3/jquery.once.min.js'></script>
<!-- IE Polyfills -->
<script>String.prototype.repeat||(String.prototype.repeat=function(t){"use strict";if(null==this)throw new TypeError("can't convert "+this+" to object");var e=""+this;if((t=+t)!=t&&(t=0),t<0)throw new RangeError("repeat count must be non-negative");if(t==1/0)throw new RangeError("repeat count must be less than infinity");if(t=Math.floor(t),0==e.length||0==t)return"";if(e.length*t>=1<<28)throw new RangeError("repeat count must not overflow maximum string size");var r=e.length*t;for(t=Math.floor(Math.log(t)/Math.log(2));t;)e+=e,t-=1;return e+=e.substring(0,r-e.length)}),Element.prototype.matches||(Element.prototype.matches=Element.prototype.msMatchesSelector||Element.prototype.webkitMatchesSelector);</script>
<script src='https://unpkg.com/once-dom@3.5.0/dist/once.min.js'></script>
<div id="wrapper"></div>
var totalDivs = 10, totalOnce = 3;
var wrapper = document.querySelector('#wrapper');
var $wrapper = jQuery(wrapper);
// uglysauce to make it work on IE11
var params = parent.window.location.search.replace('?', '').split('&');
params.forEach(function (par) {
var d = par.split('=');
if (d[0] === 'div') { totalDivs = parseInt(d[1]); }
if (d[0] === 'once') { totalOnce = parseInt(d[1]); }
})
wrapper.innerHTML = '<div class="test"></div>'.repeat(totalDivs);
// Add some info in the test name to be able to compare things
var extra = ' (div=' + totalDivs + ', once=' + totalOnce + ')';
var newjQo = 'jQuery.once ' + extra;
var newO = 'once ' + extra;
parent.document.querySelector('[data-row-for^="jQuery.once"]').setAttribute('data-row-for', newjQo);
parent.document.querySelector('[data-row-for^="once"]').setAttribute('data-row-for', newO);
// Change the test names in the string that will be eval'ed right after this one.
content = content.replace("'jQuery.once'", "'" + newjQo + "'").replace("'once'", "'" + newO + "'");
for (var i = 0; i < totalOnce; i++) {
$wrapper.find('.test').once('test' + i);
}
for (var i = 0; i < totalOnce; i++) {
once('test' + i, '.test', wrapper);
}