Longest string in array (v6)

Revision 6 of this benchmark created by Ekramul Hoque on


Preparation HTML

<script>
  a = ['adf', 'asdfdf', 'asdfdf', 'aasdfdf', 'addddddf', 'asdfdf', 'assdf', 'adfd']
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Straight
function Ls(a) {
  var l = 0;
  for (var i = 0; i < a.length; i++) {
    if (a[l].length < a[i].length) {
      l = i;
    }
  }
  return l;
}
ready
Optimized
function Lo(a) {
  var li = 0,
    ll = a[0].length !== 0 ? a[0].length - 1 : 0;
  for (var i = 0; i < a.length; i++) {
    if (a[i][ll]) {
      ll = a[i].length - 1;
      li = i;
    }
  }
  return li;
}
ready
simple
function longestString(a) {

  var len = "";
  for (j = 0; j < a.length; j++) {
    if (a[j].length > len.length) {
      len = a[j].length;
    }

  }
  return len;
}
ready

Revisions

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