joinbench

Benchmark created on


Preparation HTML

<p id="hello">Hello World</p>
<script>
  var arry = ['@callum', '@cat', 'callum', '%test', '~ishouldbefirst'];
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
anon
function get_num(c) {
 switch (c) {
 case "~":
  return 5;

 case "&":
  return 4;

 case "@":
  return 3;

 case "%":
  return 2;

 case "+":
  return 1;

 default:
  return 0;
 }
}

document.getElementById('hello').innerHTML = arry.sort(function(a, b) {
 var a_num = get_num(a.slice(0, 1));
 var b_num = get_num(b.slice(0, 1));
 if (a_num != b_num) return b_num - a_num;

 if (/[~&@%+]/.test(a_num)) {
  a_num = a.slice(1, 2);
  b_num = b.slice(1, 2);
 }

 if (typeof a_num == 'number') {
  if (typeof b_num != 'number') return 1;

  return a_num - b_num;
 }
 if (typeof b_num == 'number') return -1;

 return b_num.toLowerCase() - a_num.toLowerCase();
}).join('<br />');
ready
not
function get_num(c) {
 switch (c) {
 case "~":
  return 5;

 case "&":
  return 4;

 case "@":
  return 3;

 case "%":
  return 2;

 case "+":
  return 1;

 default:
  return 0;
 }
}

function result(a, b) {
 var a_num = get_num(a.slice(0, 1));
 var b_num = get_num(b.slice(0, 1));
 if (a_num != b_num) return b_num - a_num;

 if (/[~&@%+]/.test(a_num)) {
  a_num = a.slice(1, 2);
  b_num = b.slice(1, 2);
 }

 if (typeof a_num == 'number') {
  if (typeof b_num != 'number') return 1;

  return a_num - b_num;
 }
 if (typeof b_num == 'number') return -1;

 return b_num.toLowerCase() - a_num.toLowerCase();
}

document.getElementById('hello').innerHTML = arry.sort(result).join('<br />');
ready

Revisions

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