@gsknnft/bigint-buffer 1.3.0 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -8
- package/dist/index.cjs +205 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +203 -0
- package/dist/node.js +19 -4
- package/helper/bigint.d.ts +2 -2
- package/package.json +11 -78
- package/rollup.cjs.config.js +8 -0
- package/rollup.esm.config.js +15 -0
- package/src/index.bench.ts +119 -116
- package/src/index.spec.ts +234 -43
- package/src/index.ts +149 -39
- package/tsconfig.json +2 -5
- package/.eslintrc +0 -5
- package/dist/index.bench.d.ts +0 -1
- package/dist/index.spec.d.ts +0 -1
- package/okg.md +0 -180
- package/rollup.config.js +0 -16
- package/src/conversion/LICENSE +0 -21
- package/src/conversion/README.md +0 -48
- package/src/conversion/docs/README.md +0 -34
- package/src/conversion/docs/functions/base64ToBigint.md +0 -27
- package/src/conversion/docs/functions/bigintToBase64.md +0 -43
- package/src/conversion/docs/functions/bigintToBuf.md +0 -35
- package/src/conversion/docs/functions/bigintToHex.md +0 -43
- package/src/conversion/docs/functions/bigintToText.md +0 -31
- package/src/conversion/docs/functions/bufToBigint.md +0 -25
- package/src/conversion/docs/functions/bufToHex.md +0 -37
- package/src/conversion/docs/functions/bufToText.md +0 -27
- package/src/conversion/docs/functions/hexToBigint.md +0 -29
- package/src/conversion/docs/functions/hexToBuf.md +0 -37
- package/src/conversion/docs/functions/parseHex.md +0 -45
- package/src/conversion/docs/functions/textToBigint.md +0 -27
- package/src/conversion/docs/functions/textToBuf.md +0 -33
- package/src/conversion/docs/functions/toBigIntBE.md +0 -27
- package/src/conversion/docs/functions/toBigIntLE.md +0 -27
- package/src/conversion/docs/functions/toBufferBE.md +0 -33
- package/src/conversion/docs/functions/toBufferLE.md +0 -33
- package/src/conversion/docs/functions/validateBigIntBuffer.md +0 -15
- package/src/conversion/docs/type-aliases/TypedArray.md +0 -11
- package/src/conversion/docs/variables/isNative.md +0 -11
- package/src/conversion/example.cjs +0 -9
- package/src/conversion/example.esm.js +0 -11
- package/src/conversion/package.json +0 -182
- package/src/conversion/pnpm-lock.yaml +0 -5571
- package/src/conversion/tsconfig.rollup.json +0 -9
- package/src/conversion/typedoc.json +0 -5
- package/src/types/bindings.d.t.s +0 -4
- package/tsconfig.lint.json +0 -5
- package/vitest.config.ts +0 -10
package/package.json
CHANGED
|
@@ -1,20 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gsknnft/bigint-buffer",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"description": "bigint to buffer conversion with native support",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"browser": {
|
|
7
7
|
"./dist/node.js": "./dist/browser.js"
|
|
8
8
|
},
|
|
9
|
-
"directories": {
|
|
10
|
-
"build": "./build",
|
|
11
|
-
"dist": "./dist",
|
|
12
|
-
"docs": "./docs",
|
|
13
|
-
"src": "./src",
|
|
14
|
-
"test": "./test",
|
|
15
|
-
"benchmark": "./benchmark",
|
|
16
|
-
"mocha-ts": "./.mocha-ts"
|
|
17
|
-
},
|
|
18
9
|
"types": "dist/index.d.ts",
|
|
19
10
|
"repository": {
|
|
20
11
|
"type": "git",
|
|
@@ -32,36 +23,22 @@
|
|
|
32
23
|
],
|
|
33
24
|
"license": "Apache-2.0",
|
|
34
25
|
"dependencies": {
|
|
35
|
-
"@juanelas/base64": "^1.1.5",
|
|
36
26
|
"bindings": "^1.5.0"
|
|
37
27
|
},
|
|
38
28
|
"devDependencies": {
|
|
39
|
-
"@rollup/plugin-commonjs": "^28.0.9",
|
|
40
|
-
"@rollup/plugin-inject": "^5.0.5",
|
|
41
|
-
"@rollup/plugin-json": "^6.1.0",
|
|
42
|
-
"@rollup/plugin-multi-entry": "^7.0.1",
|
|
43
|
-
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
44
|
-
"@rollup/plugin-replace": "^6.0.2",
|
|
45
|
-
"@rollup/plugin-terser": "^0.4.4",
|
|
46
|
-
"@rollup/plugin-typescript": "^12.3.0",
|
|
47
29
|
"@types/benchmark": "^2.1.5",
|
|
48
|
-
"@types/bindings": "^1.5.5",
|
|
49
30
|
"@types/bn.js": "^5.2.0",
|
|
50
31
|
"@types/chai": "^5.2.3",
|
|
51
32
|
"@types/mocha": "^10.0.10",
|
|
52
33
|
"@types/node": "^24.9.1",
|
|
53
34
|
"benchmark": "^2.1.4",
|
|
54
35
|
"bn.js": "^5.2.2",
|
|
55
|
-
"
|
|
56
|
-
"chai": "^6.2.0",
|
|
36
|
+
"chai": "^4.1.2",
|
|
57
37
|
"coveralls": "^3.1.1",
|
|
58
38
|
"cpy-cli": "^6.0.0",
|
|
59
39
|
"cross-env": "^6.0.3",
|
|
60
|
-
"glob": "^11.0.3",
|
|
61
40
|
"gts": "^0.8.0",
|
|
62
41
|
"istanbul": "^0.4.1",
|
|
63
|
-
"jsdom": "^27.0.1",
|
|
64
|
-
"json5": "^2.2.3",
|
|
65
42
|
"karma": "^3.0.0",
|
|
66
43
|
"karma-chrome-launcher": "^2.2.0",
|
|
67
44
|
"karma-env-preprocessor": "^0.1.1",
|
|
@@ -69,39 +46,23 @@
|
|
|
69
46
|
"karma-mocha-reporter": "^2.2.5",
|
|
70
47
|
"karma-webpack": "^3.0.0",
|
|
71
48
|
"microtime": "^3.0.0",
|
|
72
|
-
"minimatch": "^10.0.3",
|
|
73
49
|
"mkdirp": "^0.5.1",
|
|
74
|
-
"mocha": "^
|
|
75
|
-
"npm-run-all": "^4.1.5",
|
|
76
|
-
"pirates": "^4.0.7",
|
|
50
|
+
"mocha": "^5.2.0",
|
|
77
51
|
"pre-commit": "^1.2.2",
|
|
78
|
-
"puppeteer": "^24.26.1",
|
|
79
|
-
"rimraf": "^6.0.1",
|
|
80
52
|
"rollup": "^4.52.5",
|
|
81
|
-
"rollup
|
|
82
|
-
"rollup-plugin-esbuild": "^6.2.1",
|
|
53
|
+
"@rollup/plugin-replace": "6.0.3",
|
|
83
54
|
"ts-loader": "^9.5.4",
|
|
84
55
|
"ts-node": "^10.9.2",
|
|
85
|
-
"ts-standard": "^12.0.2",
|
|
86
|
-
"tslib": "^2.8.1",
|
|
87
56
|
"typedoc": "^0.28.14",
|
|
88
|
-
"typedoc-plugin-markdown": "^4.9.0",
|
|
89
57
|
"typescript": "5.9.3",
|
|
90
|
-
"vite": "^7.1.12",
|
|
91
|
-
"vitest": "^4.0.3",
|
|
92
58
|
"webpack": "5.102.1",
|
|
93
59
|
"webpack-cli": "6.0.1"
|
|
94
60
|
},
|
|
95
61
|
"exports": {
|
|
96
62
|
".": {
|
|
97
|
-
"types": "./dist/index.d.ts",
|
|
98
63
|
"import": "./dist/index.js",
|
|
99
|
-
"require": "./dist/index.cjs"
|
|
100
|
-
|
|
101
|
-
"./conversion": {
|
|
102
|
-
"types": "./dist/conversion/index.d.ts",
|
|
103
|
-
"import": "./dist/conversion/index.js",
|
|
104
|
-
"require": "./dist/conversion/index.cjs"
|
|
64
|
+
"require": "./dist/index.cjs",
|
|
65
|
+
"types": "./dist/index.d.ts"
|
|
105
66
|
}
|
|
106
67
|
},
|
|
107
68
|
"engines": {
|
|
@@ -115,47 +76,19 @@
|
|
|
115
76
|
"Michael Wei <mwei@vmware.com> (https://github.com/no2chem)"
|
|
116
77
|
],
|
|
117
78
|
"scripts": {
|
|
118
|
-
"_build:cleantypes": "rimraf .types",
|
|
119
|
-
"_build:cjsAndesmPkgJsons": "node ./build/bin/post-build.js",
|
|
120
|
-
"build": "run-s lint:src build:js lint:test docs",
|
|
121
|
-
"build:js": "rollup -c build/rollup.config.mjs",
|
|
122
|
-
"postbuild:js": "run-s _build:cleantypes _build:cjsAndesmPkgJsons",
|
|
123
|
-
"clean": "rimraf .mocha-ts coverage dist .types docs",
|
|
124
|
-
"coverage": "c8 --clean --check-coverage --exclude \"{src/ts/**/*.spec.ts,src/ts/**/*.test.ts,test,test-vectors,build}\" --exclude-after-remap --reporter=text --reporter=lcov node ./build/bin/mocha-ts.js --commonjs ",
|
|
125
|
-
"docs": "node build/build.docs.js",
|
|
126
|
-
"git:add": "git add -A",
|
|
127
|
-
"lint": "ts-standard --fix",
|
|
128
|
-
"lint:src": "ts-standard --fix \"src/**/!(*.spec|*.test).ts\"",
|
|
129
|
-
"lint:test": "ts-standard --fix \"{test/**/*.ts,src/**/*.spec.ts,src/**/*.test.ts}\"",
|
|
130
|
-
"mocha-ts": "node --experimental-modules --experimental-json-modules --es-module-specifier-resolution=node ./build/bin/mocha-ts.js ",
|
|
131
|
-
"mocha-ts:cjs": "node ./build/bin/mocha-ts.js --commonjs ",
|
|
132
|
-
"mocha-ts:watch": "npm run mocha-ts:cjs -- --watch --timeout 0",
|
|
133
|
-
"mocha-ts:browser": "node build/testing/browser/index.js ",
|
|
134
|
-
"mocha-ts:browser-headless": "node build/testing/browser/index.js headless ",
|
|
135
|
-
"preversion": "run-s clean lint:src build:js lint:test coverage test:browser-headless",
|
|
136
|
-
"version": "run-s docs git:add",
|
|
137
|
-
"postversion": "git push --follow-tags",
|
|
138
|
-
"test:conv": "run-s test:node test:browser-headless",
|
|
139
|
-
"test:browser:conv": "npm run mocha-ts:browser",
|
|
140
|
-
"test:browser-headless": "npm run mocha-ts:browser-headless",
|
|
141
|
-
"test:node:conv": "run-s test:node-cjs test:node-esm",
|
|
142
|
-
"test:node-cjs": "npm run mocha-ts:cjs ",
|
|
143
|
-
"test:node-esm": "npm run mocha-ts ",
|
|
144
|
-
"watch": "npm run mocha-ts:watch ",
|
|
145
79
|
"test": "npm run test:node && npm run test:browser",
|
|
146
|
-
"coverage
|
|
80
|
+
"coverage": "istanbul cover ./test/index.js",
|
|
147
81
|
"coveralls": "npm run coverage && coveralls <coverage/lcov.info",
|
|
148
|
-
"lint
|
|
82
|
+
"lint": "gts check",
|
|
149
83
|
"install": "npm run rebuild || echo \"Couldn't build bindings. Non-native version used.\"",
|
|
150
84
|
"test:browser": "karma start karma.conf.js",
|
|
151
|
-
"test:node": "mocha
|
|
85
|
+
"test:node": "mocha build/src/**/*.spec.js --timeout 40000",
|
|
152
86
|
"benchmark": "node -r ts-node/register src/index.bench.ts",
|
|
153
87
|
"typedoc": "typedoc --out docs $(pwd)/src $(pwd)/helper --target es6 --mode file --tsconfig ./tsconfig.json --excludePrivate --excludeProtected --excludeNotExported --exclude '**/*+(spec|bench).ts'",
|
|
154
88
|
"rebuild": "node-gyp rebuild",
|
|
155
89
|
"check": "gts check",
|
|
156
|
-
"clean
|
|
157
|
-
"compile": "tsc -p . && rollup -c &&
|
|
158
|
-
"compileOG": "mkdirp dist && tsc -p . && rollup -c > dist/node.js && cross-env BROWSER=true rollup -c > dist/browser.js && cpx \"build/src/*.d.ts\" dist",
|
|
90
|
+
"clean": "gts clean",
|
|
91
|
+
"compile": "tsc -p . && rollup -c rollup.cjs.config.js && rollup -c rollup.esm.config.js && cp build/src/index.d.ts dist/index.d.ts",
|
|
159
92
|
"fix": "gts fix",
|
|
160
93
|
"pretest": "npm run compile",
|
|
161
94
|
"posttest": "npm run check"
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import replace from '@rollup/plugin-replace';
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
input: 'build/src/index.js',
|
|
5
|
+
output: {
|
|
6
|
+
file: 'dist/index.js',
|
|
7
|
+
format: 'esm'
|
|
8
|
+
},
|
|
9
|
+
external: ['bindings'],
|
|
10
|
+
plugins: [
|
|
11
|
+
replace({
|
|
12
|
+
'process.browser': 'false'
|
|
13
|
+
})
|
|
14
|
+
]
|
|
15
|
+
};
|
package/src/index.bench.ts
CHANGED
|
@@ -1,204 +1,207 @@
|
|
|
1
1
|
|
|
2
|
-
import * as benchmark from 'benchmark'
|
|
2
|
+
import * as benchmark from 'benchmark';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {toBigIntBE, toBigIntLE, toBufferBE, toBufferLE} from './index';
|
|
5
|
+
|
|
6
|
+
const BN = require('bn.js');
|
|
5
7
|
|
|
6
|
-
const BN = require('bn.js')
|
|
7
8
|
|
|
8
9
|
// This file contains the benchmark test suite. It includes the benchmark and
|
|
9
10
|
// some lightweight boilerplate code for running benchmark.js. To
|
|
10
11
|
// run the benchmarks, execute `npm run benchmark` from the package directory.
|
|
11
|
-
const suite = new benchmark.Suite()
|
|
12
|
+
const suite = new benchmark.Suite();
|
|
12
13
|
|
|
13
14
|
interface BenchmarkRun {
|
|
14
|
-
name: string
|
|
15
|
-
hz: number
|
|
16
|
-
stats: benchmark.Stats
|
|
15
|
+
name: string;
|
|
16
|
+
hz: number;
|
|
17
|
+
stats: benchmark.Stats;
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
// Tests the performance of a no-op.
|
|
20
|
-
suite.add('no-op', () => {})
|
|
21
|
+
suite.add('no-op', () => {});
|
|
21
22
|
|
|
22
23
|
// Test small strings (unaligned)
|
|
23
|
-
const smallHex = 'deadbeef'
|
|
24
|
-
const smallString = `0x${smallHex}
|
|
25
|
-
const smallBuf: Buffer = Buffer.from(smallHex, 'hex')
|
|
24
|
+
const smallHex = 'deadbeef';
|
|
25
|
+
const smallString = `0x${smallHex}`;
|
|
26
|
+
const smallBuf: Buffer = Buffer.from(smallHex, 'hex');
|
|
26
27
|
suite.add('bigint from hex string (small)', () => {
|
|
27
|
-
return BigInt(smallString)
|
|
28
|
-
})
|
|
28
|
+
return BigInt(smallString);
|
|
29
|
+
});
|
|
29
30
|
suite.add('bigint from hex string from buffer (small)', () => {
|
|
30
|
-
return BigInt(`0x${smallBuf.toString('hex')}`)
|
|
31
|
-
})
|
|
31
|
+
return BigInt(`0x${smallBuf.toString('hex')}`);
|
|
32
|
+
});
|
|
32
33
|
suite.add('BN from hex string from buffer (small)', () => {
|
|
33
|
-
return new BN(smallBuf.toString('hex'), 16)
|
|
34
|
-
})
|
|
34
|
+
return new BN(smallBuf.toString('hex'), 16);
|
|
35
|
+
});
|
|
35
36
|
suite.add('LE bigint-buffer ToBigInt (small)', () => {
|
|
36
|
-
return toBigIntLE(smallBuf)
|
|
37
|
-
})
|
|
37
|
+
return toBigIntLE(smallBuf);
|
|
38
|
+
});
|
|
38
39
|
suite.add('BE bigint-buffer ToBigInt (small)', () => {
|
|
39
|
-
return toBigIntBE(smallBuf)
|
|
40
|
-
})
|
|
40
|
+
return toBigIntBE(smallBuf);
|
|
41
|
+
});
|
|
41
42
|
|
|
42
43
|
// Test mid strings (aligned)
|
|
43
|
-
const midHex = 'badc0ffee0ddf00d'
|
|
44
|
-
const midString = `0x${midHex}
|
|
45
|
-
const midBuf: Buffer = Buffer.from(midHex, 'hex')
|
|
44
|
+
const midHex = 'badc0ffee0ddf00d';
|
|
45
|
+
const midString = `0x${midHex}`;
|
|
46
|
+
const midBuf: Buffer = Buffer.from(midHex, 'hex');
|
|
46
47
|
suite.add('bigint from hex string (mid, aligned)', () => {
|
|
47
|
-
return BigInt(midString)
|
|
48
|
-
})
|
|
48
|
+
return BigInt(midString);
|
|
49
|
+
});
|
|
49
50
|
suite.add('bigint from hex string from buffer (mid, aligned)', () => {
|
|
50
|
-
return BigInt(`0x${midBuf.toString('hex')}`)
|
|
51
|
-
})
|
|
51
|
+
return BigInt(`0x${midBuf.toString('hex')}`);
|
|
52
|
+
});
|
|
52
53
|
suite.add('BN from hex string from buffer (mid, aligned)', () => {
|
|
53
|
-
return new BN(midBuf.toString('hex'), 16)
|
|
54
|
-
})
|
|
54
|
+
return new BN(midBuf.toString('hex'), 16);
|
|
55
|
+
});
|
|
55
56
|
suite.add('LE bigint-buffer ToBigInt (mid, aligned)', () => {
|
|
56
|
-
return toBigIntLE(midBuf)
|
|
57
|
-
})
|
|
57
|
+
return toBigIntLE(midBuf);
|
|
58
|
+
});
|
|
58
59
|
suite.add('BE bigint-buffer ToBigInt (mid, aligned)', () => {
|
|
59
|
-
return toBigIntBE(midBuf)
|
|
60
|
-
})
|
|
60
|
+
return toBigIntBE(midBuf);
|
|
61
|
+
});
|
|
61
62
|
|
|
62
63
|
// Test huge strings
|
|
63
64
|
const hugeHex =
|
|
64
|
-
'badc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00d'
|
|
65
|
-
const hugeString = `0x${hugeHex}
|
|
66
|
-
const hugeBuf: Buffer = Buffer.from(hugeHex, 'hex')
|
|
65
|
+
'badc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00d';
|
|
66
|
+
const hugeString = `0x${hugeHex}`;
|
|
67
|
+
const hugeBuf: Buffer = Buffer.from(hugeHex, 'hex');
|
|
67
68
|
suite.add('bigint from hex string (huge)', () => {
|
|
68
|
-
return BigInt(hugeString)
|
|
69
|
-
})
|
|
69
|
+
return BigInt(hugeString);
|
|
70
|
+
});
|
|
70
71
|
suite.add('bigint from hex string from buffer (huge)', () => {
|
|
71
|
-
return BigInt(`0x${hugeBuf.toString('hex')}`)
|
|
72
|
-
})
|
|
72
|
+
return BigInt(`0x${hugeBuf.toString('hex')}`);
|
|
73
|
+
});
|
|
73
74
|
suite.add('BN from hex string from buffer (huge)', () => {
|
|
74
|
-
return new BN(hugeBuf.toString('hex'), 16)
|
|
75
|
-
})
|
|
75
|
+
return new BN(hugeBuf.toString('hex'), 16);
|
|
76
|
+
});
|
|
76
77
|
suite.add('LE bigint-buffer ToBigInt (huge)', () => {
|
|
77
|
-
return toBigIntLE(hugeBuf)
|
|
78
|
-
})
|
|
78
|
+
return toBigIntLE(hugeBuf);
|
|
79
|
+
});
|
|
79
80
|
suite.add('BE bigint-buffer ToBigInt (huge)', () => {
|
|
80
|
-
return toBigIntBE(hugeBuf)
|
|
81
|
-
})
|
|
81
|
+
return toBigIntBE(hugeBuf);
|
|
82
|
+
});
|
|
82
83
|
|
|
83
84
|
const bigIntToBufferWithStringBE = (int: bigint, width: number): Buffer => {
|
|
84
|
-
const hex = int.toString(16)
|
|
85
|
-
return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex')
|
|
86
|
-
}
|
|
85
|
+
const hex = int.toString(16);
|
|
86
|
+
return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
|
|
87
|
+
};
|
|
87
88
|
|
|
88
89
|
const bigIntToBufferWithStringLE = (int: bigint, width: number): Buffer => {
|
|
89
|
-
const hex = int.toString(16)
|
|
90
|
+
const hex = int.toString(16);
|
|
90
91
|
const buffer =
|
|
91
|
-
Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex')
|
|
92
|
-
buffer.reverse()
|
|
93
|
-
return buffer
|
|
94
|
-
}
|
|
92
|
+
Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
|
|
93
|
+
buffer.reverse();
|
|
94
|
+
return buffer;
|
|
95
|
+
};
|
|
95
96
|
|
|
96
97
|
// Test small toBuffer
|
|
97
|
-
const smallValue = 12345678n
|
|
98
|
+
const smallValue = 12345678n;
|
|
98
99
|
suite.add('LE bigint to hex string to buffer (small)', () => {
|
|
99
|
-
return bigIntToBufferWithStringLE(smallValue, 8)
|
|
100
|
-
})
|
|
100
|
+
return bigIntToBufferWithStringLE(smallValue, 8);
|
|
101
|
+
});
|
|
101
102
|
|
|
102
103
|
suite.add('BE bigint to hex string to buffer (small)', () => {
|
|
103
|
-
return bigIntToBufferWithStringBE(smallValue, 8)
|
|
104
|
-
})
|
|
104
|
+
return bigIntToBufferWithStringBE(smallValue, 8);
|
|
105
|
+
});
|
|
105
106
|
|
|
106
|
-
const bnSmallValue = new BN('12345678', 10)
|
|
107
|
+
const bnSmallValue = new BN('12345678', 10);
|
|
107
108
|
suite.add('BN to buffer (small)', () => {
|
|
108
|
-
return bnSmallValue.toBuffer(8)
|
|
109
|
-
})
|
|
109
|
+
return bnSmallValue.toBuffer(8);
|
|
110
|
+
});
|
|
110
111
|
|
|
111
112
|
suite.add('LE bigint-buffer to buffer (small)', () => {
|
|
112
|
-
return toBufferLE(smallValue, 8)
|
|
113
|
-
})
|
|
113
|
+
return toBufferLE(smallValue, 8);
|
|
114
|
+
});
|
|
114
115
|
|
|
115
116
|
suite.add('BE bigint-buffer to buffer (small)', () => {
|
|
116
|
-
return toBufferBE(smallValue, 8)
|
|
117
|
-
})
|
|
117
|
+
return toBufferBE(smallValue, 8);
|
|
118
|
+
});
|
|
119
|
+
|
|
118
120
|
|
|
119
121
|
// Test large toBuffer
|
|
120
122
|
const largeValue =
|
|
121
|
-
0xbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dn
|
|
123
|
+
0xbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dn;
|
|
122
124
|
suite.add('LE bigint to hex string to buffer (large)', () => {
|
|
123
|
-
return bigIntToBufferWithStringLE(largeValue, 24)
|
|
124
|
-
})
|
|
125
|
+
return bigIntToBufferWithStringLE(largeValue, 24);
|
|
126
|
+
});
|
|
125
127
|
|
|
126
128
|
suite.add('BE bigint to hex string to buffer (large)', () => {
|
|
127
|
-
return bigIntToBufferWithStringBE(largeValue, 24)
|
|
128
|
-
})
|
|
129
|
+
return bigIntToBufferWithStringBE(largeValue, 24);
|
|
130
|
+
});
|
|
129
131
|
|
|
130
132
|
const bnLargeValue = new BN(
|
|
131
|
-
|
|
132
|
-
|
|
133
|
+
'badc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00d',
|
|
134
|
+
16);
|
|
133
135
|
suite.add('BN to buffer (large)', () => {
|
|
134
|
-
return bnLargeValue.toBuffer(24)
|
|
135
|
-
})
|
|
136
|
+
return bnLargeValue.toBuffer(24);
|
|
137
|
+
});
|
|
136
138
|
|
|
137
139
|
suite.add('LE bigint-buffer to buffer (large)', () => {
|
|
138
|
-
return toBufferLE(largeValue, 24)
|
|
139
|
-
})
|
|
140
|
+
return toBufferLE(largeValue, 24);
|
|
141
|
+
});
|
|
140
142
|
|
|
141
143
|
suite.add('BE bigint-buffer to buffer (large)', () => {
|
|
142
|
-
return toBufferBE(largeValue, 24)
|
|
143
|
-
})
|
|
144
|
+
return toBufferBE(largeValue, 24);
|
|
145
|
+
});
|
|
144
146
|
|
|
145
147
|
suite.add('LE bigint to hex string to buffer (large)', () => {
|
|
146
|
-
return bigIntToBufferWithStringLE(largeValue, 8)
|
|
147
|
-
})
|
|
148
|
+
return bigIntToBufferWithStringLE(largeValue, 8);
|
|
149
|
+
});
|
|
148
150
|
|
|
149
151
|
suite.add('BE bigint to hex string to buffer (large)', () => {
|
|
150
|
-
return bigIntToBufferWithStringBE(largeValue, 8)
|
|
151
|
-
})
|
|
152
|
+
return bigIntToBufferWithStringBE(largeValue, 8);
|
|
153
|
+
});
|
|
152
154
|
|
|
153
155
|
suite.add('LE bigint-buffer to buffer (large, truncated)', () => {
|
|
154
|
-
return toBufferLE(largeValue, 8)
|
|
155
|
-
})
|
|
156
|
+
return toBufferLE(largeValue, 8);
|
|
157
|
+
});
|
|
156
158
|
|
|
157
159
|
suite.add('BE bigint-buffer to buffer (large, truncated)', () => {
|
|
158
|
-
return toBufferBE(largeValue, 8)
|
|
159
|
-
})
|
|
160
|
-
|
|
161
|
-
const b1 = Buffer.from('0123456789ABCDEF0123456789ABCDEF', 'hex')
|
|
162
|
-
const b2 = Buffer.from('0123456789ABCDEF0123456789ABCDEF', 'hex')
|
|
163
|
-
const bn1 = new BN('0123456789ABCDEF0123456789ABCDEF', 'hex')
|
|
164
|
-
const bn2 = new BN('0123456789ABCDEF0123456789ABCDEF', 'hex')
|
|
165
|
-
const n1 = 0x0123456789ABCDEF0123456789ABCDEFn
|
|
166
|
-
const n2 = 0x0123456789ABCDEF0123456789ABCDEFn
|
|
160
|
+
return toBufferBE(largeValue, 8);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
const b1 = Buffer.from('0123456789ABCDEF0123456789ABCDEF', 'hex');
|
|
164
|
+
const b2 = Buffer.from('0123456789ABCDEF0123456789ABCDEF', 'hex');
|
|
165
|
+
const bn1 = new BN('0123456789ABCDEF0123456789ABCDEF', 'hex');
|
|
166
|
+
const bn2 = new BN('0123456789ABCDEF0123456789ABCDEF', 'hex');
|
|
167
|
+
const n1 = 0x0123456789ABCDEF0123456789ABCDEFn;
|
|
168
|
+
const n2 = 0x0123456789ABCDEF0123456789ABCDEFn;
|
|
167
169
|
suite.add('Buffer equality comparison', () => {
|
|
168
|
-
return b1.compare(b2) === 0
|
|
169
|
-
})
|
|
170
|
+
return b1.compare(b2) === 0;
|
|
171
|
+
});
|
|
170
172
|
|
|
171
173
|
suite.add('BN equality comparison', () => {
|
|
172
|
-
return bn1.eq(bn2)
|
|
173
|
-
})
|
|
174
|
+
return bn1.eq(bn2);
|
|
175
|
+
});
|
|
174
176
|
|
|
175
177
|
suite.add('bigint equality comparison', () => {
|
|
176
|
-
return n1 === n2
|
|
177
|
-
})
|
|
178
|
+
return n1 === n2;
|
|
179
|
+
});
|
|
178
180
|
|
|
179
181
|
suite.add('BN multiply', () => {
|
|
180
|
-
return bn1.mul(bn2)
|
|
181
|
-
})
|
|
182
|
+
return bn1.mul(bn2);
|
|
183
|
+
});
|
|
182
184
|
|
|
183
185
|
suite.add('bigint multiply', () => {
|
|
184
|
-
return n1 * n2
|
|
185
|
-
})
|
|
186
|
+
return n1 * n2;
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
//#endregion
|
|
186
190
|
|
|
187
|
-
// #endregion
|
|
188
191
|
|
|
189
192
|
// Reporter for each benchmark
|
|
190
193
|
suite.on('cycle', (event: benchmark.Event) => {
|
|
191
|
-
const benchmarkRun: BenchmarkRun = event.target as BenchmarkRun
|
|
192
|
-
const stats = benchmarkRun.stats
|
|
193
|
-
const meanInNanos = (stats.mean * 1000000000).toFixed(2)
|
|
194
|
-
const stdDevInNanos = (stats.deviation * 1000000000).toFixed(3)
|
|
195
|
-
const runs = stats.sample.length
|
|
196
|
-
const ops = benchmarkRun.hz.toFixed(benchmarkRun.hz < 100 ? 2 : 0)
|
|
197
|
-
const err = stats.rme.toFixed(2)
|
|
194
|
+
const benchmarkRun: BenchmarkRun = event.target as BenchmarkRun;
|
|
195
|
+
const stats = benchmarkRun.stats as benchmark.Stats;
|
|
196
|
+
const meanInNanos = (stats.mean * 1000000000).toFixed(2);
|
|
197
|
+
const stdDevInNanos = (stats.deviation * 1000000000).toFixed(3);
|
|
198
|
+
const runs = stats.sample.length;
|
|
199
|
+
const ops = benchmarkRun.hz.toFixed(benchmarkRun.hz < 100 ? 2 : 0);
|
|
200
|
+
const err = stats.rme.toFixed(2);
|
|
198
201
|
|
|
199
202
|
console.log(`${benchmarkRun.name}: ${ops}±${err}% ops/s ${meanInNanos}±${
|
|
200
|
-
stdDevInNanos} ns/op (${runs} run${runs === 0 ? '' : 's'})`)
|
|
201
|
-
})
|
|
203
|
+
stdDevInNanos} ns/op (${runs} run${runs === 0 ? '' : 's'})`);
|
|
204
|
+
});
|
|
202
205
|
|
|
203
206
|
// Runs the test suite
|
|
204
|
-
suite.run()
|
|
207
|
+
suite.run();
|