On Mon, Feb 22, 2010 at 00:28, Dimitar Christoff <[email protected]>wrote:
> > Why not use RegExp?
> >
> speed. this will be slower.
>
I'm not sure about this. hasClass require string-operation anyway and with
RegExp, you can directly set the correct class without further comparing.
I made a small benchmark comparing different methods posted here:
- Method1 (my method):
- return this.set('class',new RegExp(klass).exec(this.get('class')));
- Method2 (Roman):
- return this.set('class', this.get('class').split(' ').filter(
function(c){return c == this },klass)[0]);
- Method3 (hazlema):
- return this.hasClass(klass) ? this.set("class", klass) : this.erase
("class");
Results (time per operation):
- Method1: 0.000265s
- Method2: 0.000297s
- Method3: 0.000279s
Looks like if my method is slightly faster...
benchmark: http://mootools.net/shell/uDHj4/
Yann