One-liner for you:
Math.max.apply(Math, $.map(array, function (el) { return el.length }));
Working example: http://jsfiddle.net/5SDBx/
You can do it without jQuery in newer browsers (or even older browsers with a compatibility implementation of Array.prototype.map) too:
Math.max.apply(Math, array.map(function (el) { return el.length }));
Answer from Andy E on Stack OverflowOne-liner for you:
Math.max.apply(Math, $.map(array, function (el) { return el.length }));
Working example: http://jsfiddle.net/5SDBx/
You can do it without jQuery in newer browsers (or even older browsers with a compatibility implementation of Array.prototype.map) too:
Math.max.apply(Math, array.map(function (el) { return el.length }));
A new answer to an old question: in ES6 you can do even shorter:
Math.max(...array.map(el => el.length));
The maximum length until "it gets sluggish" is totally dependent on your target machine and your actual code, so you'll need to test on that (those) platform(s) to see what is acceptable.
However, the maximum length of an array according to the ECMA-262 5th Edition specification is bound by an unsigned 32-bit integer due to the ToUint32 abstract operation, so the longest possible array could have 232-1 = 4,294,967,295 = 4.29 billion elements.
No need to trim the array, simply address it as a circular buffer (index % maxlen). This will ensure it never goes over the limit (implementing a circular buffer means that once you get to the end you wrap around to the beginning again - not possible to overrun the end of the array).
For example:
var container = new Array ();
var maxlen = 100;
var index = 0;
// 'store' 1538 items (only the last 'maxlen' items are kept)
for (var i=0; i<1538; i++) {
container [index++ % maxlen] = "storing" + i;
}
// get element at index 11 (you want the 11th item in the array)
eleventh = container [(index + 11) % maxlen];
// get element at index 11 (you want the 11th item in the array)
thirtyfifth = container [(index + 35) % maxlen];
// print out all 100 elements that we have left in the array, note
// that it doesn't matter if we address past 100 - circular buffer
// so we'll simply get back to the beginning if we do that.
for (i=0; i<200; i++) {
document.write (container[(index + i) % maxlen] + "<br>\n");
}
Available since Javascript 1.8/ECMAScript 5 and available in most older browsers:
var longest = arr.reduce(
function (a, b) {
return a.length > b.length ? a : b;
}
);
Otherwise, a safe alternative:
var longest = arr.sort(
function (a, b) {
return b.length - a.length;
}
)[0];
A new answer to an old question: in ES6 you can do shorter:
Math.max(...(x.map(el => el.length)));
» npm install @stdlib/constants-array-max-array-length