Skip to content

perf: drop un-flatten-tree dep & optimize Tree Data utils by ~3x perf#2601

Open
ghiscoding wants to merge 4 commits into
masterfrom
perf/drop-un-flatten-tree
Open

perf: drop un-flatten-tree dep & optimize Tree Data utils by ~3x perf#2601
ghiscoding wants to merge 4 commits into
masterfrom
perf/drop-un-flatten-tree

Conversation

@ghiscoding

@ghiscoding ghiscoding commented Jun 8, 2026

Copy link
Copy Markdown
Owner

vibe coded with copilot using Claude Sonnet 4.6

Summary

Removes the un-flatten-tree third-party dependency from @slickgrid-universal/common and rewrites or optimizes all tree data utility functions for significantly better performance, particularly at scale (50k–250k nodes). This makes the Tree Data to parse about ~3x faster when converting from flat to tree and vice versa (flatten/unflatten) which is how Tree Data was implemented in SlickGrid (when using Tree Data in SlickGrid we always keep a flat data copy and its equivalent tree data).


Changes

package.json

  • Removed "un-flatten-tree" from dependencies

utilities.ts

flattenToParentChildArray (previously used un-flatten-tree)

  • Replaced the library call with a custom O(n) iterative DFS using two parallel stacks (nodeStack + parentStack) — avoids tuple allocation per node
  • Uses a shallow copy per node ({ ...node } then delete flatNode[childrenPropName]) to preserve the original hierarchical dataset intact (shared object references between flat and hierarchical arrays require this)
  • ~3x faster than the previous implementation at 250k nodes (~35ms vs ~100ms)

findItemInTreeStructure (previously recursive)

  • Replaced with an O(n) iterative DFS using a stack — eliminates call-stack depth risk on deep trees and is faster than the two-pass recursive approach

unflattenParentChildArrayToTree

  • Replaced the plain object lookup with a Map — avoids prototype-chain overhead and Object.keys() allocation
  • Replaced forEach with indexed for loops

addTreeLevelByMutation

  • Replaced getTreeDataOptionPropName() calls with direct options.childrenPropName access
  • Replaced forEach with indexed for loop; cached children to a local variable (single Array.isArray check)

addTreeLevelAndAggregatorsByMutation

  • Same optimizations as above; also caches isParent to avoid double Array.isArray check

treeData.service.ts

  • Added 5 cached instance fields (_childrenPropName, _collapsedPropName, _hasChildrenPropName, _lazyLoadingPropName, _levelPropName) initialized once in init() — eliminates ~20 repeated getTreeDataOptionPropName() call sites across the service, each of which previously chained two getter calls plus a switch statement
  • Removed all per-call getTreeDataOptionPropName() lookups from: handleOnCellClick, handleOnKeyDown, toggleTreeDataCollapse, updateToggledItem, applyToggledItemStateChanges, initHierarchicalTree, recalculateTreeTotals, getItemCount
  • toggleAllFlatDataItems: inlined the updateToggledItem(item, collapsing, false) body directly into the loop — eliminates one function call + two getTreeDataOptionPropName lookups per item during full collapse/expand of large datasets

utilities.spec.ts

  • Updated 4 unflattenParentChildArrayToTree test expectations to match Map insertion order (previously relied on plain-object auto-sorting of integer keys)
  • Updated 3 flattenToParentChildArray tests to reflect that children property is no longer present on flat items (was previously leaking through)
  • Added 7 new unit tests: 3 for addTreeLevelByMutation, 2 for addTreeLevelAndAggregatorsByMutation, 2 for findItemInTreeStructure

@codecov

codecov Bot commented Jun 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.0%. Comparing base (9dcc1ff) to head (570bb6b).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2601   +/-   ##
=======================================
  Coverage   100.0%   100.0%           
=======================================
  Files         199      199           
  Lines       25168    25182   +14     
  Branches     8908     8909    +1     
=======================================
+ Hits        25168    25182   +14     
Flag Coverage Δ
angular 100.0% <ø> (ø)
universal 100.0% <100.0%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Jun 8, 2026

Copy link
Copy Markdown
angular-slickgrid

npm i https://pkg.pr.new/angular-slickgrid@2601

aurelia-slickgrid

npm i https://pkg.pr.new/aurelia-slickgrid@2601

slickgrid-react

npm i https://pkg.pr.new/slickgrid-react@2601

slickgrid-vue

npm i https://pkg.pr.new/slickgrid-vue@2601

@slickgrid-universal/angular-row-detail-plugin

npm i https://pkg.pr.new/@slickgrid-universal/angular-row-detail-plugin@2601

@slickgrid-universal/aurelia-row-detail-plugin

npm i https://pkg.pr.new/@slickgrid-universal/aurelia-row-detail-plugin@2601

@slickgrid-universal/react-row-detail-plugin

npm i https://pkg.pr.new/@slickgrid-universal/react-row-detail-plugin@2601

@slickgrid-universal/vue-row-detail-plugin

npm i https://pkg.pr.new/@slickgrid-universal/vue-row-detail-plugin@2601

@slickgrid-universal/binding

npm i https://pkg.pr.new/@slickgrid-universal/binding@2601

@slickgrid-universal/common

npm i https://pkg.pr.new/@slickgrid-universal/common@2601

@slickgrid-universal/composite-editor-component

npm i https://pkg.pr.new/@slickgrid-universal/composite-editor-component@2601

@slickgrid-universal/custom-footer-component

npm i https://pkg.pr.new/@slickgrid-universal/custom-footer-component@2601

@slickgrid-universal/custom-tooltip-plugin

npm i https://pkg.pr.new/@slickgrid-universal/custom-tooltip-plugin@2601

@slickgrid-universal/empty-warning-component

npm i https://pkg.pr.new/@slickgrid-universal/empty-warning-component@2601

@slickgrid-universal/event-pub-sub

npm i https://pkg.pr.new/@slickgrid-universal/event-pub-sub@2601

@slickgrid-universal/excel-export

npm i https://pkg.pr.new/@slickgrid-universal/excel-export@2601

@slickgrid-universal/graphql

npm i https://pkg.pr.new/@slickgrid-universal/graphql@2601

@slickgrid-universal/odata

npm i https://pkg.pr.new/@slickgrid-universal/odata@2601

@slickgrid-universal/pagination-component

npm i https://pkg.pr.new/@slickgrid-universal/pagination-component@2601

@slickgrid-universal/pdf-export

npm i https://pkg.pr.new/@slickgrid-universal/pdf-export@2601

@slickgrid-universal/row-detail-view-plugin

npm i https://pkg.pr.new/@slickgrid-universal/row-detail-view-plugin@2601

@slickgrid-universal/rxjs-observable

npm i https://pkg.pr.new/@slickgrid-universal/rxjs-observable@2601

@slickgrid-universal/sql

npm i https://pkg.pr.new/@slickgrid-universal/sql@2601

@slickgrid-universal/text-export

npm i https://pkg.pr.new/@slickgrid-universal/text-export@2601

@slickgrid-universal/utils

npm i https://pkg.pr.new/@slickgrid-universal/utils@2601

@slickgrid-universal/vanilla-bundle

npm i https://pkg.pr.new/@slickgrid-universal/vanilla-bundle@2601

@slickgrid-universal/vanilla-force-bundle

npm i https://pkg.pr.new/@slickgrid-universal/vanilla-force-bundle@2601

commit: 570bb6b

@ghiscoding

ghiscoding commented Jun 8, 2026

Copy link
Copy Markdown
Owner Author

@zewa666 another win for the AI usage... by looking at the dependency "un-flatten-tree", I found this benchmark created by a user a few years ago: https://github.com/philipstanislaus/array-to-tree-benchmarks

then after asking the AI if I could switch to the more performant lib, Claude AI actually identified that we could use native code (aka drop dependency) and improve the perf by about ~3x (or 300%) with O(n) instead of O(n2)... wow this is just awesome... go ahead Claude 🚀 That's also great because that was 1 of the last package that wasn't fully ESM, which is why it was on my list of TODOs eventually.

my company increased our plan to Premium for a month because of all the token outcry, many employees got 75% token usage on day 1, so after a few days they bumped our account to Premium but it's just for a month, so better take advantage of it while it last 😆 .... EDIT: ah wait, they changed their mind and reverted to regular access and now I'm out of credit, it was fun while it lasted lol


also just let me know when you're done reviewing the other 2 PRs

@ghiscoding ghiscoding changed the title perf: drop un-flatten-tree dep & optimize Tree Data utilities by ~3x perf: drop un-flatten-tree dep & optimize Tree Data perf by factor of ~3x Jun 8, 2026
@ghiscoding ghiscoding changed the title perf: drop un-flatten-tree dep & optimize Tree Data perf by factor of ~3x perf: drop un-flatten-tree dep & optimize Tree Data utils by ~3x perf Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant