ruilisi / fortune-sheet

A drop-in javascript spreadsheet library that provides rich features like Excel and Google Sheets
https://ruilisi.github.io/fortune-sheet-docs/
MIT License
2.54k stars 227 forks source link

Formula calculation optimisations #545

Closed sanchit3008 closed 5 months ago

sanchit3008 commented 6 months ago

Resolves #486

Right now, pasting formulae into sheets is roughly an $O(n^2)$ operation due to the linear search loop on the entire calcChain being run for each formula update.

This PR uses a Set instead of the calcChain array to optimise the search operation into $O(log n)$ - hence converting the entire operation to $O(nlogn)$ more or less. The set is computed only once for the entire operation, which makes paste operations less than ~15 rows more expensive, but for more rows it is many orders of magnitude faster.

Here are my findings for the calculation processing time (blue is the current code in master, red is the code in this branch) -

image