Skip to content
GitLab
Projects Groups Topics Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Register
  • Sign in
  • erp5 erp5
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributor statistics
    • Graph
    • Compare revisions
  • Merge requests 142
    • Merge requests 142
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Jobs
  • Commits
Collapse sidebar
  • nexedinexedi
  • erp5erp5
  • Merge requests
  • !2107

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

  • Review changes

  • Download
  • Patches
  • Plain diff
Merged Jérome Perrin requested to merge fix/isEmptyisFast into master May 26, 2025
  • Overview 11
  • Commits 2
  • Pipelines 3
  • Changes 1

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.

Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: fix/isEmptyisFast
GitLab Nexedi Edition | About GitLab | About Nexedi | 沪ICP备2021021310号-2 | 沪ICP备2021021310号-7