Skip to content

Conversation

@Nodonisko
Copy link

ethjs-unit

Migrate to native JS bigint.

Before opening a pull request, please ensure:

  • You have followed our contributing guidelines
  • Pull request has tests (we are going for 100% coverage!)
  • Code is well-commented, linted and follows project conventions
  • Documentation is updated (if necessary)
  • Internal code generators and templates are updated (if necessary)
  • Description explains the issue/use-case resolved and auto-closes related issues

Be kind to code reviewers, please try to keep pull requests as small and focused as possible :)

IMPORTANT: By submitting a patch, you agree to allow the project
owners to license your work under the terms of the MIT License.

sethkfman pushed a commit to MetaMask/utils that referenced this pull request Aug 27, 2025
This PR is migrating state of `@metamask/ethjs-unit` from this PR
MetaMask/ethjs-unit#24 to Typescript and moving
it to this repo.

I migrated all existing test cases and also added bunch of new ones.

Just migration to `bigint` improved performance already quite a bit and
I also added few more optimizations to make it even faster.

Here are some results:


#### 1. `getValueOfUnit` - **92.4% faster (13.1x speedup)**

- **Old**: 0.0008ms average, 1,186,924 ops/sec
- **New**: 0.0001ms average, 15,559,761 ops/sec
- **Memory usage**: Minimal impact

#### 2. `numberToString` - **Mixed Results**

- **String inputs**: 43.9% faster (1.8x speedup)
- **Number inputs**: 211.7% slower\*
- **Memory usage**: Negligible difference
- \*Note: Regression due to additional BigInt support in benchmark and
in absolute numbers it's still so fast that is negligible (0.0001ms vs
0.0003ms)

#### 3. `fromWei` - **41.8% to 71.9% faster**

- **Small values**: 41.8% faster (1.7x speedup)
- **Gwei values**: 47.4% faster (1.9x speedup)
- **Ether values**: 47.4% faster (1.9x speedup)
- **Large values**: 52.0% faster (2.1x speedup)
- **With options**: 71.9% faster (3.6x speedup)
- **Memory usage**: 63.8% to 99.2% reduction

#### 4. `toWei` - **55.2% to 79.0% faster**

- **Small values**: 71.2% faster (3.5x speedup)
- **Medium values**: 73.9% faster (3.8x speedup)
- **Large values**: 79.0% faster (4.8x speedup)
- **Decimal values**: 55.2% faster (2.2x speedup)
- **Memory usage**: 63.4% reduction
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