ordinals in 140 bytes (v3)

Revision 3 of this benchmark created by furf on


Description

return ordinal value of a number

Setup

function ordinal (a){return["th","st","nd","rd"][(a=~~(a<0?-a:a)%100)>10&&a<14||(a%=10)>3?0:a]}
    
    function ordinalWithRegExp (a){return[,"st","nd","rd"][/1?.$/.exec(a)]||"th"}
    
    function ordinalMapReduce (b){b=(b+"").split("").slice(-2).reduce(function(d,c){if(1==d){return 0;}else{return c>3?0:c;}});return["th","st","nd","rd"][b];}

Test runner

Ready to run.

Testing in
TestOps/sec
ordinal
for (var i = -123; i < 123; ++i) {
  ordinal(i);
}
ready
ordinal with regexp
for (var i = -123; i < 123; ++i) {
  ordinalWithRegExp(i);
}
ready
ordinal with map reduce
for (var i = -123; i < 123; ++i) {
  ordinalMapReduce(i);
}
ready

Revisions

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