Skip to content

Instantly share code, notes, and snippets.

@52cik
Created March 13, 2016 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 52cik/33d503f49cc67a70f24b to your computer and use it in GitHub Desktop.
Save 52cik/33d503f49cc67a70f24b to your computer and use it in GitHub Desktop.
hasClass 测试
var some = Array.prototype.some;
function hasClass1(name){
if (!name) {
return false
}
return some.call(this, function(el){
return this.test(el.className)
}, new RegExp('(^|\\s)' + name + '(\\s|$)'));
}
function hasClass2(name){
if (!name) {
return false
}
return some.call(this, function(el){
return (' ' + el.className + ' ').indexOf(this) > -1
}, ' ' + name + ' ');
}
// test demos.
var nodes = [{className:'aa bb'}, {className:'bb cc'}, {className:' dd ee ff '}];
var ret1 = hasClass1.call(nodes, 'aa');
var ret2 = hasClass2.call(nodes, 'aa');
console.log(ret1, ret2);
var n = 1e7;
console.time('reg');
for(var i=0; i<n; i++) {
hasClass1.call(nodes, 'aa')
}
console.timeEnd('reg');
console.time('str');
for(var i=0; i<n; i++) {
hasClass2.call(nodes, 'aa')
}
console.timeEnd('str');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment