> - Method1: 0.000265s
> - Method2: 0.000297s
> - Method3: 0.000279s
> Looks like if my method is slightly faster...
Mmm, something is wrong with your benchmark.
Comparing your regex
[f1] t.set('class',new RegExp('base_class').exec(t.get('class')));
with the straightforward
[f2] t.hasClass('base_class') ? t.set('class','base_class') :
t.erase('class');
averaging over 10 runs of 1000 function calls each, the regex approach
is markedly slower.
[f1]:
.016 when base_class is present on the element
.012 when base_class is not present
[f2]:
.007 when base_class is present
.004 when base_class is not present
You can eke out more performance by compiling the RegEx, but the basic
approach still beats it out significantly, 50-100% faster depending on
whether the target class is present.
[f1-compiled]:
.011 when base_class is present on the element
.008 when base_class is not present
See http://mootools.net/shell/B8pPc/7/.
- S.