Conversation
4d4f1fc to
bbb0f18
Compare
The BN suffix was a leftover from the bn.js library.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
floatShifts normalized each operand with toBns, then isHex re-ran toBns on the already-normalized strings (~4 toBns calls per op). Replace isHex with a string-only isHexStr that skips toBns and uses a single regex instead of up to six toLowerCase().includes() allocations. Also gate the scientific-notation regex behind an 'e' check and drop validate's split() allocations. Correctness unchanged (195 tests pass); roughly halves core operation cost. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2 tasks
precisionAdjust (floor/ceil/round) routed single-digit rounding checks through the full public gte/gt API (each a floatShifts + parse round-trip). Compare the rounding digit directly and test the remainder with a regex instead. trimEnd ran up to four regex passes per call; rewrite it as one index scan. Correctness unchanged (195 tests pass); about 1.6x faster on rounding-heavy and decimal-heavy workloads. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CHANGELOG
Does this branch warrant an entry to the CHANGELOG?
Dependencies
none
Description
Replaces the
bn.jsdependency with nativeBigInt, and removes a redundant-normalization hot spot infloatShifts.What changed
BigIntthrough aparseBigInthelper that handles signed hex (-0x100), whichBigInt()rejects. Thebn.jsruntime dependency is dropped.floatShiftsno longer re-normalizes its operands.isHexwas re-runningtoBnson already-normalized strings (about 4toBnscalls per operation); it now uses a string-onlyisHexStr(one regex, notoLowerCaseallocations).toBnsruns the scientific-notation regex only when the input containse, andvalidatechecks for duplicate./-withoutsplit()allocations.BigInt). Version 5.0.0.Performance
320k mixed operations (add/sub/mul, div with precision, comparisons, toFixed, hex), measured on the React Native Hermes VM and on V8 (the plugin WebView engine class). Results are identical (checksum verified).
About 1.7x faster on Hermes and 2.1x faster on V8, with one fewer dependency. The two wins are independent: native
BigIntis faster thanbn.jsfor parse, arithmetic, andtoStringon both engines (bn.jsis pure-JS 26-bit-word math,BigIntruns in the engine), and the leanerfloatShiftsremoves work the published build was already avoiding but the repo source was not.Note
High Risk
Core numeric behavior now depends on native
BigIntand integer division semantics instead ofbn.js, which is a breaking major bump for a library used in money-related string math.Overview
5.0.0 drops the
bn.jsruntime dependency and routes all arithmetic and comparisons through nativeBigInt, with aparseBigInthelper so signed hex like-0x100still parses. Public API shape is unchanged; Node >= 10.4 and TypeScriptES2020are now required.Hot-path work is trimmed:
floatShiftsuses string-onlyisHexStrinstead of re-runningtoBnsinside hex detection;toBnsonly runs the scientific-notation regex when the input containse;validateavoidssplitallocations.precisionAdjustcompares rounding digits in-place instead of callinggte/gt, andtrimEndis a single index scan instead of multiple regex passes.Tooling is narrowed to TypeScript-only lint (JSX/React/Babel ESLint paths and plugins removed), legacy
.babelrcand CircleCI config deleted, and dev dependencies refreshed (TypeScript 5, pinned ESLint stack).Reviewed by Cursor Bugbot for commit 0061c39. Bugbot is set up for automated code reviews on this repo. Configure here.