Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Benchmark = require('benchmark')
- const BigNumber = require('bignumber.js').BigNumber
- const Long = require('long')
- const ITERATIONS_PER_CALL = 1000
- const TEST_DATA_STRINGS = new Array(ITERATIONS_PER_CALL).fill(null).map((v, i) => String(i))
- const TEST_DATA_BUFFERS = TEST_DATA_STRINGS.map(str => Buffer.from(new BigNumber(str).toString(16).padStart(16, '0'), 'hex'))
- const HIGH_WORD_MULTIPLIER = 0x100000000
- const target = new Buffer(8)
- const suite = new Benchmark.Suite()
- suite
- .add('Long serialize', function () {
- for (let i = 0; i < ITERATIONS_PER_CALL; i++) {
- const amount = Long.fromString(TEST_DATA_STRINGS[i], true)
- target.writeUInt32BE(amount.getHighBitsUnsigned())
- target.writeUInt32BE(amount.getLowBitsUnsigned(), 4)
- }
- })
- .add('Bignumber.js serialize', function () {
- for (let i = 0; i < ITERATIONS_PER_CALL; i++) {
- const uint64 = new BigNumber(TEST_DATA_STRINGS[i])
- target.writeUInt32BE(uint64.dividedToIntegerBy(HIGH_WORD_MULTIPLIER).toNumber())
- target.writeUInt32BE(uint64.modulo(HIGH_WORD_MULTIPLIER).toNumber(), 4)
- }
- })
- .add('Long deserialize', function () {
- for (let i = 0; i < ITERATIONS_PER_CALL; i++) {
- const highBits = TEST_DATA_BUFFERS[i].readUInt32BE()
- const lowBits = TEST_DATA_BUFFERS[i].readUInt32BE(4)
- Long.fromBits(+lowBits, +highBits, true).toString()
- }
- })
- .add('Bignumber.js deserialize', function () {
- for (let i = 0; i < ITERATIONS_PER_CALL; i++) {
- const highBits = TEST_DATA_BUFFERS[i].readUInt32BE()
- const lowBits = TEST_DATA_BUFFERS[i].readUInt32BE(4)
- const uint64 = new BigNumber(highBits).times(HIGH_WORD_MULTIPLIER).plus(lowBits)
- uint64.toString(10)
- }
- })
- .on('cycle', function(event) {
- console.log(String(event.target))
- })
- .on('complete', function() {
- console.log('Fastest is ' + this.filter('fastest').map('name'));
- })
- .on('error', function (err) {
- console.log('Error:', err)
- })
- .run({ async: true })
Add Comment
Please, Sign In to add comment