The way you've used RegExp will also match partial matches; you look
for "rump" and get a match on "scrump"... not good. False positives.

@Yann: The contains method you listed is the Array method - not String
one which looks like this:

contains: function(string, separator){
        return (separator) ? (separator + this + separator).indexOf(separator
+ string + separator) > -1 : this.indexOf(string) > -1;
}

Combining that code with the className property you'd get:

removeAllClassExcept: function(klass) {
    this.className = ((' ' + this.className + ' ').indexOf(' ' + klass
+ ' ')>-1) ? klass : '';
    return this;
}

http://mootools.net/shell/bmfGu/3/





On Feb 24, 4:00 am, Yann Leretaille <[email protected]>
wrote:
> > But your benchmark still isn't balanced. For example, fn [1b] is using
> > more  pure  JS/DOM functions and is implemented on Array, while fn [4]
> > is using Moo overhead and is implemented on Element.
>
> Really? [4] used className, so i wanted to see how big the change was when
> using className in [1a] too (and for pure JS i think i should replace the
> ".each", but this wont make a big difference i think). Actually using pure
> JS for "contains" makes [4] 30x times faster than before, while hasClass
> does the same:
>
>     hasClass: function(className){
>         return this.className.contains(className, ' ');
>     },
>
>     contains: function(item, from){
>         return this.indexOf(item, from) != -1;
>     },
> Anyway its a question of milliseconds and i don't think it will ever matter
> in productive enviroment. So what is the best method to programm such a type
> of functions? The moo way or pureJS way?
> Moo code is more portable&durable but in some situations it could be a
> performance killer. In this case RegExp+moo's get/set is still faster then
> all other methods using moo code whenever its possible, mainly because it
> needs less of it. I think it will always be a compromise between speed and
> the ease&usefullness of moo, unless somebody writes a moo2pureJS compiler.
> (Would be a nice experiment, though).
>
> Well, enough benchmarking for today, i hope i didn't annoyed anybody by
> blowing up this thread with all this stuff.
> Yann
>
> On Wed, Feb 24, 2010 at 00:52, Sanford Whiteman <[email protected]
>
> > wrote:
> > But your benchmark still isn't balanced. For example, fn [1b] is using
> > more  pure  JS/DOM functions and is implemented on Array, while fn [4]
> > is using Moo overhead and is implemented on Element.
>
> > The most apt competitor to your className.RegExp is className.indexOf.
> > I adjusted fn [4] accordingly. I think you'll see that [4] wins out at
> > n=10000,  1000,  100 and is about the same at n=10 (I don't think n=10
> > is  appropriate  to  test,  since  there is no "smoothing" effect in a
> > benchmark that small).
>
> >http://mootools.net/shell/bmfGu/3/
>
> > -- S.

Reply via email to