ordinals in 140 bytes

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"}

Test runner

Ready to run.

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

Revisions

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