jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
测试classList与模拟实现的性能差别
<div id="t" class="one two">
</div>
//对dom节点的class进行操作
//对于不支持element.classList特性的浏览器进行模拟
var getClassList = function (ele) {
// if (ele.classList) return ele.classList;
var classList = {};
var classes = ele.className.split(/\s+/);
//索引下标赋值,成为类似数组对象
for (var i = 0, l = classes.length; i < l; i++) {
classList[i] = classes[i];
}
classList.length = classes[0] === '' ? 0 : classes.length;
classList.update = function () {
ele.className = [].join.call(this, ' ');
}
classList.contains = function (name, returnIndex) {
for (var i = 0, l = this.length; i < l; i++) {
if (this[i] === name) return returnIndex ? i : true;
}
return false;
}
classList.add = function (name) {
if (!this.contains(name)) {
this[this.length] = name;
this.length++;
this.update();
}
}
classList.remove = function (name) {
var index = this.contains(name, true);
if (index !== false) {
for (var l = this.length; index < l - 1; index++) {
this[index] = this[index + 1];
}
delete this[index];
this.length--;
this.update();
}
}
classList.toggle = function (name) {
if (this.contains(name)) {
this.remove(name);
} else {
this.add(name);
}
}
classList.toString = function () {
var strArr = [];
for (var i = 0, l = classList.length; i < l; i++) {
strArr.push(classList[i]);
}
return strArr.join();
}
return classList;
}
Ready to run.
Test | Ops/sec | |
---|---|---|
Native |
| ready |
none-Native |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.