currency sort enhancement

currency sort enhancement

mattsimersonmattsimerson Posts: 1Questions: 0Answers: 0
edited January 2012 in Plug-ins
In the currency sort plugin is a block of code that looks like this:

/* Remove the currency sign */
x = x.substring( 1 );
y = y.substring( 1 );

That's great when one assumes that currency will always be rendered with a prefix. In my case, that assumption was invalid. My alteration tests to make sure the first character is not a number. It works whether a prefix exists or not.

if (isNaN(x.substring(0,1))){ x = x.substring( 1 ) };
if (isNaN(y.substring(0,1))){ y = y.substring( 1 ) };

My first inclination was to use a regexp and strip out all characters that aren't in the class [0-9\-.]. That would get rid of spaces, pre and postfix currency characters ($.02, as well as 2¢), commas, and any other stray markup that wandered into the field. With a simple regexp like that, there's no backtracking so performance should be reasonable. Is there an easy way to test the performance using a regexp instead of this replace + substring solution? (I currently test performance by loading something my 1st gen iPad. If it loads quickly enough, I consider it "good enough.")

Replies

  • allanallan Posts: 61,710Questions: 1Answers: 10,103 Site admin
    Very nice - thanks for posting the enhancement!

    The way I tend to test JS performance these days is with http://jsperf.com - I'll try to find some time to write up some suitable tests for this and the regex method soon.

    Allan
This discussion has been closed.