Created
March 21, 2022 18:22
-
-
Save heyqbnk/10e27ce728dda4219545b0780bc2222b to your computer and use it in GitHub Desktop.
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
const Benchmark = require('benchmark') | |
const {BigInt: JSBI, add, subtract, divide, multiply} = require('jsbi'); | |
const suite = new Benchmark.Suite | |
// native numbers | |
let num_a, num_b | |
// BigInt numbers | |
let bi_a, bi_b | |
// JSBI numbers | |
let jsbi_a, jsbi_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() | |
bi_a = BigInt(num_a) | |
bi_b = BigInt(num_b) | |
jsbi_a = JSBI(num_a) | |
jsbi_b = JSBI(num_b) | |
} | |
const genNewBiggerInputs = () => { | |
const str_a = randBiggerInt() | |
const str_b = randBiggerInt() | |
bi_a = BigInt(str_a) | |
bi_b = BigInt(str_b) | |
jsbi_a = JSBI(str_a) | |
jsbi_b = JSBI(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('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) | |
.add('JSBigInt#add', () => add(jsbi_a, jsbi_b), options) | |
.add('JSBigInt#sub', () => subtract(jsbi_a, jsbi_b), options) | |
.add('JSBigInt#div', () => divide(jsbi_a, jsbi_b), options) | |
.add('JSBigInt#mul', () => multiply(jsbi_a, jsbi_b), options) | |
.add('JSBigInt#add (n > 2**64)', () => add(jsbi_a, jsbi_b), bigOptions) | |
.add('JSBigInt#sub (n > 2**64)', () => subtract(jsbi_a, jsbi_b), bigOptions) | |
.add('JSBigInt#div (n > 2**64)', () => divide(jsbi_a, jsbi_b), bigOptions) | |
.add('JSBigInt#mul (n > 2**64)', () => multiply(jsbi_a, jsbi_b), bigOptions) | |
.on('cycle', (evt) => console.log(String(evt.target))) | |
.run({async: true}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment