Skip to content

fix: 30% smaler bundle - Replace mathjs with lightweight formula evaluator in admin UI- R19#2448

Closed
dirkwa wants to merge 2 commits intoSignalK:masterfrom
dirkwa:remove-mathjs-from-admin-ui
Closed

fix: 30% smaler bundle - Replace mathjs with lightweight formula evaluator in admin UI- R19#2448
dirkwa wants to merge 2 commits intoSignalK:masterfrom
dirkwa:remove-mathjs-from-admin-ui

Conversation

@dirkwa
Copy link
Contributor

@dirkwa dirkwa commented Mar 14, 2026

Summary

mathjs was introduced in 6251163 ("Implement display units system in metadata", #2225) to evaluate unit conversion formulas. It's a full computer algebra system (~18MB on disk, 396 transitive packages) but the formulas are simple arithmetic like value * 1.94384 or value - 273.15.

This replaces mathjs with cached new Function() constructors. The formulas are server-controlled, not user input, so this is safe.

Bundle reduction: 2,148 KB → 1,506 KB (−30%), removes 152 transitive packages.

Test plan

  • Verified unit conversions work correctly in the Data Browser (knots, degrees, feet, etc.)
  • Verified Unit Preferences settings page loads and presets apply correctly

Note:

@dirkwa
Copy link
Contributor Author

dirkwa commented Mar 14, 2026

R16 fails - it has own formulas, so I will rebase this on R19 sole #2441

dirkwa added 2 commits March 15, 2026 07:26
Rename the React 19 admin UI package from
@signalk/server-admin-ui-react19 to @signalk/server-admin-ui,
replacing the old React 16 admin UI. The -react19 suffix was
only needed during the coexistence/testing period.

The SIGNALK_ADMIN_UI env var toggle is no longer needed.
Legacy R16 plugins continue to work via the bridge in
dynamicutilities.ts. The /@signalk/server-admin-ui route
still redirects to /admin/ for backwards compatibility.
mathjs added ~660KB to the admin UI bundle for simple arithmetic
formula evaluation. Use cached Function constructors instead, as
the conversion formulas are server-controlled and contain only
basic arithmetic with a single `value` variable.
@dirkwa dirkwa force-pushed the remove-mathjs-from-admin-ui branch from 42046cc to 634c4ce Compare March 14, 2026 19:49
@dirkwa dirkwa changed the title Replace mathjs with lightweight formula evaluator in admin UI fix: 30% smaler bundle - Replace mathjs with lightweight formula evaluator in admin UI- R19 Mar 15, 2026
let cached = compiledFormulaCache.get(formula)
if (!cached) {
cached = compile(formula)
cached = new Function('value', `return (${formula})`) as (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We chose mathjs explicitly so that we are not evaluating unvalidated user supplied code.

If we want to get rid of mathjs we can instead generate a library that will have all the known source-target conversions (there are only a finite number of them) as static js functions and do just a lookup on source and target.

This won't allow adding arbitratry dynamic target units, but will support almost all practical use cases in Data Browser.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re: server controlled: yes, the conversions are today server controlled, but I think this is just a vector I don't want to open.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really? All this balloning just to know how much my tank holds in tea spoons?

Copy link
Contributor Author

@dirkwa dirkwa Mar 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re: server controlled: yes, the conversions are today server controlled, but I think this is just a vector I don't want to open.

I briefly thought about it, as option, because the server has math.js anyway.

Balooning the bundle is not a good idea. I see the impact already on my PI 4 in the wifi. It gets boaring slow.

Let me change this to server side.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we already went over that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And how do we want to bring the performance back of RPI over wifi?
Split the bundle?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried the split and are quite impressed.

Initial loading is way quicker and a better expirience than waiting in front of a white browser being unsure if anything crashed.

#2453

@tkurki tkurki closed this Mar 15, 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.

2 participants