Last active
February 8, 2023 05:22
-
-
Save wemeetagain/73a92ceb5f942b940898eea0aa578546 to your computer and use it in GitHub Desktop.
BigInt vs BN.js vs number benchmark
This file contains 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
const Benchmark = require('benchmark') | |
const BN = require('bn.js') | |
const suite = new Benchmark.Suite | |
// native numbers | |
let num_a, num_b | |
// BN.js numbers | |
let bn_a, bn_b | |
// native BigInt numbers | |
let bi_a, bi_b | |
const randSmallInt = () => Math.floor(Math.random() * 2**24) | |
const randBiggerInt = () => "" + Math.floor(Math.random() * 2**48) + Math.floor(Math.random() * 2**48) | |
const genNewSmallInputs = () => { | |
num_a = randSmallInt() | |
num_b = randSmallInt() | |
bn_a = new BN(num_a) | |
bn_b = new BN(num_b) | |
bi_a = BigInt(num_a) | |
bi_b = BigInt(num_b) | |
} | |
const genNewBiggerInputs = () => { | |
const str_a = randBiggerInt() | |
const str_b = randBiggerInt() | |
bn_a = new BN(str_a) | |
bn_b = new BN(str_b) | |
bi_a = BigInt(str_a) | |
bi_b = BigInt(str_b) | |
} | |
const options = { | |
onStart: genNewSmallInputs, | |
onCycle: genNewSmallInputs, | |
} | |
const bigOptions = { | |
onStart: genNewBiggerInputs, | |
onCycle: genNewBiggerInputs, | |
} | |
suite | |
.add('number#add', () => num_a + num_b, options) | |
.add('number#sub', () => num_a - num_b, options) | |
.add('number#div', () => Math.floor(num_a / num_b), options) | |
.add('number#mul', () => num_a * num_b, options) | |
.add('BN#add', () => bn_a.add(bn_b), options) | |
.add('BN#sub', () => bn_a.sub(bn_b), options) | |
.add('BN#div', () => bn_a.div(bn_b), options) | |
.add('BN#mul', () => bn_a.mul(bn_b), options) | |
.add('BN#add (n > 2**64)', () => bn_a.add(bn_b), bigOptions) | |
.add('BN#sub (n > 2**64)', () => bn_a.sub(bn_b), bigOptions) | |
.add('BN#div (n > 2**64)', () => bn_a.div(bn_b), bigOptions) | |
.add('BN#mul (n > 2**64)', () => bn_a.mul(bn_b), bigOptions) | |
.add('BigInt#add', () => bi_a + bi_b, options) | |
.add('BigInt#sub', () => bi_a - bi_b, options) | |
.add('BigInt#div', () => bi_a / bi_b, options) | |
.add('BigInt#mul', () => bi_a * bi_b, options) | |
.add('BigInt#add (n > 2**64)', () => bi_a + bi_b, bigOptions) | |
.add('BigInt#sub (n > 2**64)', () => bi_a - bi_b, bigOptions) | |
.add('BigInt#div (n > 2**64)', () => bi_a / bi_b, bigOptions) | |
.add('BigInt#mul (n > 2**64)', () => bi_a * bi_b, bigOptions) | |
.on('cycle', (evt) => console.log(String(evt.target))) | |
.run({async: true}) | |
/* | |
$ node --version | |
v10.15.1 | |
$ node bigint_vs_bn_vs_number.js | |
number#add x 166,680,025 ops/sec ±0.44% (92 runs sampled) | |
number#sub x 165,210,303 ops/sec ±3.40% (85 runs sampled) | |
number#div x 170,510,248 ops/sec ±0.63% (90 runs sampled) | |
number#mul x 163,694,640 ops/sec ±1.42% (91 runs sampled) | |
BN#add x 39,612,100 ops/sec ±0.93% (88 runs sampled) | |
BN#sub x 17,208,730 ops/sec ±1.63% (78 runs sampled) | |
BN#div x 18,231,185 ops/sec ±7.30% (65 runs sampled) | |
BN#mul x 59,932,956 ops/sec ±1.53% (87 runs sampled) | |
BN#add (n > 2**64) x 24,662,255 ops/sec ±0.40% (92 runs sampled) | |
BN#sub (n > 2**64) x 12,091,169 ops/sec ±0.95% (86 runs sampled) | |
BN#div (n > 2**64) x 5,488,495 ops/sec ±20.32% (39 runs sampled) | |
BN#mul (n > 2**64) x 8,006,484 ops/sec ±1.04% (88 runs sampled) | |
BigInt#add x 19,542,139 ops/sec ±1.06% (86 runs sampled) | |
BigInt#sub x 23,921,336 ops/sec ±0.82% (91 runs sampled) | |
BigInt#div x 25,085,055 ops/sec ±3.21% (74 runs sampled) | |
BigInt#mul x 17,035,338 ops/sec ±0.86% (88 runs sampled) | |
BigInt#add (n > 2**64) x 18,395,735 ops/sec ±0.98% (86 runs sampled) | |
BigInt#sub (n > 2**64) x 22,845,381 ops/sec ±0.86% (92 runs sampled) | |
BigInt#div (n > 2**64) x 14,991,941 ops/sec ±17.13% (47 runs sampled) | |
BigInt#mul (n > 2**64) x 12,937,009 ops/sec ±0.38% (90 runs sampled) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
M1 / macos 12 clearly shows that native is the way to go