currency sort enhancement
currency sort enhancement
mattsimerson
Posts: 1Questions: 0Answers: 0
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.")
/* 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.")
This discussion has been closed.
Replies
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