hasClass with className.match

JavaScript performance comparison

Test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="test" class="bar foo baz">test wrapper</div>
<script>
Benchmark.prototype.setup = function() {
    var test = document.getElementById( 'test' ),
        $test = $( '#test' );
   
    var myHasClass = function( toTest, obj ) {
         return obj[0].className.match( new RegExp( '\\b' + toTest + '\\b', 'g' ) );
    };
};
</script>

Preparation code output

test wrapper

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
jQuery hasClass
$test.hasClass( 'foo' );
pending…
className match RegExp
myHasClass( 'foo', $test);
pending…
native
test.className.match( /foo/ );
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment