Camel Cased jQuery Data Attributes

Benchmark created by Kin Blas on


Description

Test the performance of translating namespaced data attributes to camelcase. Figure out if returning a cached copy from the camelcase function is worth the overhead, versus just checking the cache before the camelcase call.

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<script>

var props = "prop prop-with-dashes long-prop-name-with-dashes".split(" "), propStr, i, v;
var p2CamelCaseDict = {};
var p2CamelCaseDict2 = {};

function propToNSCamelCase( prop )
{
    return $.camelCase( "foo-" + prop );
}

function propToNSCamelCaseCache( prop )
{
    return p2CamelCaseDict[ prop ] || ( p2CamelCaseDict[ prop ] = $.camelCase( "foo-" + prop ) );
}

function propToNSCamelCaseCache2( prop )
{
    return ( p2CamelCaseDict2[ prop ] = $.camelCase( "foo-" + prop ) );
}

</script>

Setup

p2CamelCaseDict = {};
    p2CamelCaseDict2 = {};

Test runner

Ready to run.

Testing in
TestOps/sec
propToNSCamelCase( prop )
for ( i = 0; i < props.length; i++ ) {
    v = propToNSCamelCase( props[ i ] );
}
ready
propToNSCamelCaseCache(prop)
for ( i = 0; i < props.length; i++ ) {
    v = propToNSCamelCaseCache( props[ i ] );
}
ready
propToNSCamelCaseCache2(prop)
for ( i = 0; i < props.length; i++ ) {
    propStr = props[ i ];
    v = p2CamelCaseDict2[ propStr ] || ( propToNSCamelCaseCache2( props[ i ] ) );
}
ready

Revisions

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

  • Revision 1: published by Kin Blas on