Skip to content

core/gadget_global.js: optimize `isEmpty` for strings

Jérome Perrin requested to merge fix/isEmptyisFast into master

isEmpty is slow for long strings, because it uses Object.keys(str) on strings, which builds a new Array and can even cause RangeError: Too many properties to enumerate.

console.time('isEmpty');
try {
    isEmpty('long str'.repeat(1e6));
} catch (e) {
    console.error(e);
}
console.timeEnd("isEmpty");
isEmpty: 3652.545166015625 ms

for very long strings it even causes an error:

console.time('isEmpty');
try {
    isEmpty('very long str'.repeat(1e6));
} catch (e) {
    console.error(e);
}
console.timeEnd("isEmpty");
RangeError: Too many properties to enumerate
    at Object.keys (<anonymous>)
    at isEmpty (gadget_global.js:19:19)
    at <anonymous>:3:5
isEmpty: 2905.382080078125 ms

we noticed this on Spreadsheet’s preview tab in ERP5JS, for a 1Mo spreadsheet. On chrome there is an error after a few seconds, on firefox this seems to freeze the browser. With the updated version optimized for strings, same snippet is instant and does not error anymore.

Merge request reports