@gsknnft/bigint-buffer 1.4.1 → 1.4.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 +3 -3
- package/build/Release/bigint_buffer.exp +0 -0
- package/build/Release/bigint_buffer.iobj +0 -0
- package/build/Release/bigint_buffer.ipdb +0 -0
- package/build/Release/bigint_buffer.lib +0 -0
- package/build/Release/bigint_buffer.node +0 -0
- package/build/Release/bigint_buffer.pdb +0 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.node.recipe +11 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/CL.command.1.tlog +0 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/CL.read.1.tlog +0 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/CL.write.1.tlog +0 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/Cl.items.tlog +2 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/bigint_buffer.lastbuildstate +2 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/link.command.1.tlog +0 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/link.read.1.tlog +0 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/link.secondary.1.tlog +5 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/link.write.1.tlog +0 -0
- package/build/Release/obj/bigint_buffer/src/bigint-buffer.obj +0 -0
- package/build/Release/obj/bigint_buffer/win_delay_load_hook.obj +0 -0
- package/build/bigint_buffer.vcxproj +148 -0
- package/build/bigint_buffer.vcxproj.filters +67 -0
- package/build/binding.sln +19 -0
- package/build/config.gypi +522 -0
- package/dist/types/conversion/src/ts/index.d.ts +168 -0
- package/dist/types/conversion/test/bigintToBase64.test.d.ts +1 -0
- package/dist/types/conversion/test/bigintToBuf.test.d.ts +1 -0
- package/dist/types/conversion/test/bigintToHex.test.d.ts +1 -0
- package/dist/types/conversion/test/bigintToText.test.d.ts +1 -0
- package/dist/types/conversion/test/bufToBigint.test.d.ts +1 -0
- package/dist/types/conversion/test/hexToBigint.test.d.ts +1 -0
- package/dist/types/conversion/test/hexToBuf.test.d.ts +1 -0
- package/dist/types/conversion/test/parseHex.test.d.ts +1 -0
- package/dist/types/conversion/test/setup.test.d.ts +1 -0
- package/dist/types/conversion/test/textToBuf.test.d.ts +1 -0
- package/package.json +42 -27
- package/src/bigint-buffer.c +203 -0
- package/src/bigint-buffer.test.ts +11 -0
- package/src/conversion/.github/workflows/build-and-test.yml +116 -0
- package/src/conversion/CODE_OF_CONDUCT.md +134 -0
- package/src/conversion/LICENSE +21 -0
- package/src/conversion/README.md +48 -0
- package/src/conversion/docs/README.md +34 -0
- package/src/conversion/docs/functions/base64ToBigint.md +27 -0
- package/src/conversion/docs/functions/bigintToBase64.md +43 -0
- package/src/conversion/docs/functions/bigintToBuf.md +35 -0
- package/src/conversion/docs/functions/bigintToHex.md +43 -0
- package/src/conversion/docs/functions/bigintToText.md +31 -0
- package/src/conversion/docs/functions/bufToBigint.md +25 -0
- package/src/conversion/docs/functions/bufToHex.md +37 -0
- package/src/conversion/docs/functions/bufToText.md +27 -0
- package/src/conversion/docs/functions/hexToBigint.md +29 -0
- package/src/conversion/docs/functions/hexToBuf.md +37 -0
- package/src/conversion/docs/functions/parseHex.md +45 -0
- package/src/conversion/docs/functions/textToBigint.md +27 -0
- package/src/conversion/docs/functions/textToBuf.md +33 -0
- package/src/conversion/docs/functions/toBigIntBE.md +27 -0
- package/src/conversion/docs/functions/toBigIntLE.md +27 -0
- package/src/conversion/docs/functions/toBufferBE.md +33 -0
- package/src/conversion/docs/functions/toBufferLE.md +33 -0
- package/src/conversion/docs/functions/validateBigIntBuffer.md +15 -0
- package/src/conversion/docs/type-aliases/TypedArray.md +11 -0
- package/src/conversion/docs/variables/isNative.md +11 -0
- package/src/conversion/example.cjs +9 -0
- package/src/conversion/example.esm.js +11 -0
- package/src/conversion/index.ts +1 -0
- package/src/conversion/package.json +163 -0
- package/src/conversion/src/docs/index.md +47 -0
- package/src/conversion/src/ts/index.ts +514 -0
- package/src/conversion/test/bigintToBase64.test.ts +37 -0
- package/src/conversion/test/bigintToBuf.test.ts +43 -0
- package/src/conversion/test/bigintToHex.test.ts +52 -0
- package/src/conversion/test/bigintToText.test.ts +30 -0
- package/src/conversion/test/bufToBigint.test.ts +20 -0
- package/src/conversion/test/hexToBigint.test.ts +22 -0
- package/src/conversion/test/hexToBuf.test.ts +39 -0
- package/src/conversion/test/parseHex.test.ts +35 -0
- package/src/conversion/test/setup.test.ts +9 -0
- package/src/conversion/test/textToBuf.test.ts +26 -0
- package/src/conversion/tsconfig.json +57 -0
- package/src/conversion/tsconfig.rollup.json +9 -0
- package/src/conversion/typedoc.json +5 -0
- package/src/conversion/types/bindings.d.t.s +4 -0
- package/src/conversion/vite.config.ts +10 -0
- package/src/conversion/vitest.config.ts +15 -0
- package/src/index.bench.ts +206 -0
- package/src/index.spec.ts +318 -0
- package/src/index.ts +215 -0
- package/.travis.yml +0 -51
- package/PR_TEMPLATE.md +0 -53
- package/WHY_BIGINT.md +0 -127
- package/benchmark.md +0 -38
- package/eslint.config.ts +0 -12
- package/karma.conf.js +0 -62
- package/pnpm-workspace.yaml +0 -14
- package/rollup.cjs.config.js +0 -13
- package/rollup.conversion.cjs.config.js +0 -13
- package/rollup.conversion.esm.config.js +0 -24
- package/rollup.esm.config.js +0 -24
- package/tsconfig.tsbuildinfo +0 -1
- package/vite.config.ts +0 -44
- package/vitest.config.ts +0 -20
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import * as bc from '#pkg';
|
|
2
|
+
import {describe, expect, it} from 'vitest';
|
|
3
|
+
|
|
4
|
+
describe('hexToBigint', () => {
|
|
5
|
+
const inputs = [
|
|
6
|
+
{bi: BigInt(1), hex: '1'}, {bi: BigInt(31), hex: '1F'}, {
|
|
7
|
+
bi: BigInt('12485413541784539569456874935679853424678352483761'),
|
|
8
|
+
hex: '88af94e6b1e99f8bf3b01edb619caaa656A5c75b1'
|
|
9
|
+
}
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
describe('hexToBigint', () => {
|
|
13
|
+
for (const input of inputs) {
|
|
14
|
+
describe(`hexToBigint(${input.hex})`, () => {
|
|
15
|
+
it(`should return ${input.bi}`, () => {
|
|
16
|
+
const ret = bc.hexToBigint(input.hex);
|
|
17
|
+
expect(ret).to.equal(input.bi);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import * as bc from '#pkg';
|
|
2
|
+
import {describe, expect, it} from 'vitest';
|
|
3
|
+
|
|
4
|
+
describe('hexToBuf', () => {
|
|
5
|
+
const tests = [
|
|
6
|
+
{buf: new Uint8Array([9, 255]), hex: '09ff'},
|
|
7
|
+
{buf: new Uint16Array([5, 256]), hex: '05000001'},
|
|
8
|
+
{buf: new ArrayBuffer(2), hex: '000'},
|
|
9
|
+
{buf: new Uint8Array([1, 1]), hex: '0x101'},
|
|
10
|
+
{buf: new Uint8Array([1, 1, 1]), hex: '10101'}
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
describe('hexToBuf and bufToHex', () => {
|
|
14
|
+
for (const test of tests) {
|
|
15
|
+
describe(`bufToHex([${(new Uint8Array(test.buf)).toString()}])`, () => {
|
|
16
|
+
const byteLength = test.buf.byteLength;
|
|
17
|
+
const expected = bc.parseHex(test.hex, false, byteLength);
|
|
18
|
+
it(`should return ${String(expected)}`, () => {
|
|
19
|
+
const ret = bc.bufToHex(test.buf);
|
|
20
|
+
expect(ret).to.equal(expected);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
describe(`bufToHex(hexToBuf(${test.hex}))`, () => {
|
|
24
|
+
const byteLength = test.buf.byteLength;
|
|
25
|
+
const expected = bc.parseHex(test.hex, false, byteLength);
|
|
26
|
+
it(`should return ${String(expected)}`, () => {
|
|
27
|
+
const buf = bc.hexToBuf(test.hex);
|
|
28
|
+
const ret = bc.bufToHex(buf);
|
|
29
|
+
expect(ret).to.equal(expected);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
describe('hexToBuf(\'12412fgt3\')', () => {
|
|
33
|
+
it('should throw RangeError', () => {
|
|
34
|
+
expect(() => bc.hexToBuf('12412fgt3')).to.throw(RangeError);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as bc from '#pkg';
|
|
2
|
+
import {describe, expect, it} from 'vitest';
|
|
3
|
+
|
|
4
|
+
describe('parseHex', () => {
|
|
5
|
+
const vectors = [
|
|
6
|
+
{input: '0x234FE678', output: '234FE678'},
|
|
7
|
+
{input: '234FE687', output: '234FE687'},
|
|
8
|
+
{input: '0x123546146f23A2', output: '123546146f23A2'}
|
|
9
|
+
];
|
|
10
|
+
for (const vector of vectors) {
|
|
11
|
+
it(`parseHex('${vector.input}') should return '${vector.output}'`, () => {
|
|
12
|
+
const ret = bc.parseHex(vector.input);
|
|
13
|
+
expect(ret).to.equal(vector.output);
|
|
14
|
+
});
|
|
15
|
+
it(`parseHex('${vector.input}', true) should return '0x${vector.output}'`,
|
|
16
|
+
() => {
|
|
17
|
+
const ret = bc.parseHex(vector.input, true);
|
|
18
|
+
expect(ret).to.equal('0x' + vector.output);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
it('parseHex(\'adge3\') should throw error', () => {
|
|
22
|
+
expect(() => {
|
|
23
|
+
bc.parseHex('adge3');
|
|
24
|
+
}).to.throw(RangeError);
|
|
25
|
+
});
|
|
26
|
+
it('parseHex(\'1287a3\', undefined, 4) should return \'001287a3\'', () => {
|
|
27
|
+
const ret = bc.parseHex('1287a3', undefined, 4);
|
|
28
|
+
expect(ret).to.equal('001287a3');
|
|
29
|
+
});
|
|
30
|
+
it('parseHex(\'1287542fe21\', true, 4) should throw error', () => {
|
|
31
|
+
expect(() => {
|
|
32
|
+
bc.parseHex('1287542fe21', true, 4);
|
|
33
|
+
}).to.throw(RangeError);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as bc from "#pkg";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
|
|
4
|
+
describe("textToBuf", () => {
|
|
5
|
+
const inputs = [
|
|
6
|
+
"Hello World",
|
|
7
|
+
"Apañarse por qué?",
|
|
8
|
+
`Lorem ipsum dolor sit amet, consectetur adipisci tempor incidunt ut labore et dolore magna aliqua veniam, quis nostrud exercitation ullamcorpor s commodo consequat. Duis autem vel eum irrure esse molestiae consequat, vel illum dolore eu fugi et iusto odio dignissim qui blandit praesent luptat exceptur sint occaecat cupiditat non provident, deserunt mollit anim id est laborum et dolor fuga distinct. Nam liber tempor cum soluta nobis elige quod maxim placeat facer possim omnis volupt
|
|
9
|
+
|
|
10
|
+
Lorem ipsum dolor si amet, consectetur adipiscing incidunt ut labore et dolore magna aliquam erat nostrud exercitation ullamcorper suscipit laboris nis duis autem vel eum irure dolor in reprehenderit i, dolore eu fugiat nulla pariatur. At vero eos et accusa praesant luptatum delenit aigue duos dolor et mole provident, simil tempor sunt in culpa qui officia de fuga. Et harumd dereud facilis est er expedit disti eligend optio congue nihil impedit doming id quod assumenda est, omnis dolor repellend. Temporibud
|
|
11
|
+
|
|
12
|
+
Lorem ipsum dolor si amet, consectetur adipiscing incidunt ut labore et dolore magna aliquam erat nostrud exercitation ullamcorper suscipit laboris nis duis autem vel eum irure dolor in reprehenderit i dolore eu fugiat nulla pariatur. At vero eos et accus praesant luptatum delenit aigue duos dolor et mol provident, simil tempor sunt in culpa qui officia de fuga. Et harumd dereud facilis est er expedit disti eligend oprio congue nihil impedit doming id quod assumenda est, omnis dolor repellend. Temporibud`,
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
let returnArrayBuffer = false;
|
|
16
|
+
|
|
17
|
+
for (const input of inputs) {
|
|
18
|
+
describe(`bufToText(textToBuf(${input.substring(0, 8)}...))`, () => {
|
|
19
|
+
it(`should return ${input.substring(0, 8)}...`, () => {
|
|
20
|
+
returnArrayBuffer = returnArrayBuffer ? false : true;
|
|
21
|
+
const ret = bc.bufToText(bc.textToBuf(input, returnArrayBuffer));
|
|
22
|
+
expect(ret).to.equal(input);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/tsconfig",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": ".",
|
|
5
|
+
"target": "ESNext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', 'ES2023', 'ES2023' or 'ESNEXT'. */
|
|
6
|
+
"module": "NodeNext",
|
|
7
|
+
// "lib": [ "es2020" ], /* Specify library files to be included in the compilation. */
|
|
8
|
+
"allowJs": true, /* Allow javascript files to be compiled. */
|
|
9
|
+
"checkJs": true, /* Report errors in .js files. */
|
|
10
|
+
// "jsx": "preserve", /* Specify JSX code generation: 'react', 'react-jsx', 'react-jsxdev', 'preserve' or 'react-native'. */
|
|
11
|
+
"strict": true,
|
|
12
|
+
"declaration": true,
|
|
13
|
+
"removeComments": true,
|
|
14
|
+
"typeRoots": ["../types", "../../node_modules/@types", "node_modules/@types"], /* Additional Checks */
|
|
15
|
+
// "noUnusedLocals": true, /* Report errors on unused locals. */
|
|
16
|
+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
|
|
17
|
+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
|
|
18
|
+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
|
19
|
+
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
|
|
20
|
+
"tsBuildInfoFile": "build/main.tsbuildinfo",
|
|
21
|
+
"lib": [ "esnext", "dom" ],
|
|
22
|
+
"types": ["node"],
|
|
23
|
+
|
|
24
|
+
/* Module Resolution Options */
|
|
25
|
+
"moduleResolution": "NodeNext", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
|
26
|
+
"baseUrl": ".", /* Base directory to resolve non-absolute module names. */
|
|
27
|
+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
|
28
|
+
// "rootDir": ".",
|
|
29
|
+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
|
30
|
+
// "typeRoots": [ "node_modules/@types", "build/typings" ], /* List of folders to include type definitions from. */
|
|
31
|
+
// "types": [], /* Type declaration files to be included in compilation. */
|
|
32
|
+
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
|
|
33
|
+
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
|
|
34
|
+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
|
|
35
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
36
|
+
|
|
37
|
+
/* Experimental Options */
|
|
38
|
+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
|
|
39
|
+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
|
|
40
|
+
/* Advanced Options */
|
|
41
|
+
"skipLibCheck": true, /* Skip type checking of declaration files. */
|
|
42
|
+
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
|
|
43
|
+
"resolveJsonModule": true,
|
|
44
|
+
"paths": {
|
|
45
|
+
"#pkg": ["./src/ts/index.ts"]
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"exclude": [
|
|
49
|
+
"node_modules",
|
|
50
|
+
"build",
|
|
51
|
+
"src/conversion/.dst"
|
|
52
|
+
],
|
|
53
|
+
"include": [
|
|
54
|
+
"src/**/*.ts",
|
|
55
|
+
"test/**/*.ts"
|
|
56
|
+
]
|
|
57
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { defineConfig } from "vitest/config";
|
|
2
|
+
import path from "path";
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
test: {
|
|
6
|
+
globals: true,
|
|
7
|
+
environment: "node",
|
|
8
|
+
include: ["src/conversion/test/**/*.test.ts"],
|
|
9
|
+
},
|
|
10
|
+
resolve: {
|
|
11
|
+
alias: {
|
|
12
|
+
"#pkg": path.resolve(__dirname, "src/ts/index.ts"),
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
});
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
|
|
2
|
+
import benchmark from 'benchmark';
|
|
3
|
+
import BN from 'bn.js';
|
|
4
|
+
|
|
5
|
+
import {toBigIntBE, toBigIntLE, toBufferBE, toBufferLE} from './index';
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
// This file contains the benchmark test suite. It includes the benchmark and
|
|
9
|
+
// some lightweight boilerplate code for running benchmark.js. To
|
|
10
|
+
// run the benchmarks, execute `npm run benchmark` from the package directory.
|
|
11
|
+
const suite = new benchmark.Suite();
|
|
12
|
+
|
|
13
|
+
interface BenchmarkRun {
|
|
14
|
+
name: string;
|
|
15
|
+
hz: number;
|
|
16
|
+
stats: benchmark.Stats;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Tests the performance of a no-op.
|
|
20
|
+
suite.add('no-op', () => {});
|
|
21
|
+
|
|
22
|
+
// Test small strings (unaligned)
|
|
23
|
+
const smallHex = 'deadbeef';
|
|
24
|
+
const smallString = `0x${smallHex}`;
|
|
25
|
+
const smallBuf: Buffer = Buffer.from(smallHex, 'hex');
|
|
26
|
+
suite.add('bigint from hex string (small)', () => {
|
|
27
|
+
return BigInt(smallString);
|
|
28
|
+
});
|
|
29
|
+
suite.add('bigint from hex string from buffer (small)', () => {
|
|
30
|
+
return BigInt(`0x${smallBuf.toString('hex')}`);
|
|
31
|
+
});
|
|
32
|
+
suite.add('BN from hex string from buffer (small)', () => {
|
|
33
|
+
return new BN(smallBuf.toString('hex'), 16);
|
|
34
|
+
});
|
|
35
|
+
suite.add('LE bigint-buffer ToBigInt (small)', () => {
|
|
36
|
+
return toBigIntLE(smallBuf);
|
|
37
|
+
});
|
|
38
|
+
suite.add('BE bigint-buffer ToBigInt (small)', () => {
|
|
39
|
+
return toBigIntBE(smallBuf);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// Test mid strings (aligned)
|
|
43
|
+
const midHex = 'badc0ffee0ddf00d';
|
|
44
|
+
const midString = `0x${midHex}`;
|
|
45
|
+
const midBuf: Buffer = Buffer.from(midHex, 'hex');
|
|
46
|
+
suite.add('bigint from hex string (mid, aligned)', () => {
|
|
47
|
+
return BigInt(midString);
|
|
48
|
+
});
|
|
49
|
+
suite.add('bigint from hex string from buffer (mid, aligned)', () => {
|
|
50
|
+
return BigInt(`0x${midBuf.toString('hex')}`);
|
|
51
|
+
});
|
|
52
|
+
suite.add('BN from hex string from buffer (mid, aligned)', () => {
|
|
53
|
+
return new BN(midBuf.toString('hex'), 16);
|
|
54
|
+
});
|
|
55
|
+
suite.add('LE bigint-buffer ToBigInt (mid, aligned)', () => {
|
|
56
|
+
return toBigIntLE(midBuf);
|
|
57
|
+
});
|
|
58
|
+
suite.add('BE bigint-buffer ToBigInt (mid, aligned)', () => {
|
|
59
|
+
return toBigIntBE(midBuf);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Test huge strings
|
|
63
|
+
const hugeHex =
|
|
64
|
+
'badc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00d';
|
|
65
|
+
const hugeString = `0x${hugeHex}`;
|
|
66
|
+
const hugeBuf: Buffer = Buffer.from(hugeHex, 'hex');
|
|
67
|
+
suite.add('bigint from hex string (huge)', () => {
|
|
68
|
+
return BigInt(hugeString);
|
|
69
|
+
});
|
|
70
|
+
suite.add('bigint from hex string from buffer (huge)', () => {
|
|
71
|
+
return BigInt(`0x${hugeBuf.toString('hex')}`);
|
|
72
|
+
});
|
|
73
|
+
suite.add('BN from hex string from buffer (huge)', () => {
|
|
74
|
+
return new BN(hugeBuf.toString('hex'), 16);
|
|
75
|
+
});
|
|
76
|
+
suite.add('LE bigint-buffer ToBigInt (huge)', () => {
|
|
77
|
+
return toBigIntLE(hugeBuf);
|
|
78
|
+
});
|
|
79
|
+
suite.add('BE bigint-buffer ToBigInt (huge)', () => {
|
|
80
|
+
return toBigIntBE(hugeBuf);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
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
|
+
};
|
|
87
|
+
|
|
88
|
+
const bigIntToBufferWithStringLE = (int: bigint, width: number): Buffer => {
|
|
89
|
+
const hex = int.toString(16);
|
|
90
|
+
const buffer =
|
|
91
|
+
Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
|
|
92
|
+
buffer.reverse();
|
|
93
|
+
return buffer;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// Test small toBuffer
|
|
97
|
+
const smallValue = 12345678n;
|
|
98
|
+
suite.add('LE bigint to hex string to buffer (small)', () => {
|
|
99
|
+
return bigIntToBufferWithStringLE(smallValue, 8);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
suite.add('BE bigint to hex string to buffer (small)', () => {
|
|
103
|
+
return bigIntToBufferWithStringBE(smallValue, 8);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const bnSmallValue = new BN('12345678', 10);
|
|
107
|
+
suite.add('BN to buffer (small)', () => {
|
|
108
|
+
return bnSmallValue.toArrayLike(Buffer, 'be', 8);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
suite.add('LE bigint-buffer to buffer (small)', () => {
|
|
112
|
+
return toBufferLE(smallValue, 8);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
suite.add('BE bigint-buffer to buffer (small)', () => {
|
|
116
|
+
return toBufferBE(smallValue, 8);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
// Test large toBuffer
|
|
121
|
+
const largeValue =
|
|
122
|
+
0xbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dn;
|
|
123
|
+
suite.add('LE bigint to hex string to buffer (large)', () => {
|
|
124
|
+
return bigIntToBufferWithStringLE(largeValue, 24);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
suite.add('BE bigint to hex string to buffer (large)', () => {
|
|
128
|
+
return bigIntToBufferWithStringBE(largeValue, 24);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const bnLargeValue = new BN(
|
|
132
|
+
'badc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00d',
|
|
133
|
+
16);
|
|
134
|
+
suite.add('BN to buffer (large)', () => {
|
|
135
|
+
return bnLargeValue.toArrayLike(Buffer, 'be', 24);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
suite.add('LE bigint-buffer to buffer (large)', () => {
|
|
139
|
+
return toBufferLE(largeValue, 24);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
suite.add('BE bigint-buffer to buffer (large)', () => {
|
|
143
|
+
return toBufferBE(largeValue, 24);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
suite.add('LE bigint to hex string to buffer (large)', () => {
|
|
147
|
+
return bigIntToBufferWithStringLE(largeValue, 8);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
suite.add('BE bigint to hex string to buffer (large)', () => {
|
|
151
|
+
return bigIntToBufferWithStringBE(largeValue, 8);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
suite.add('LE bigint-buffer to buffer (large, truncated)', () => {
|
|
155
|
+
return toBufferLE(largeValue, 8);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
suite.add('BE bigint-buffer to buffer (large, truncated)', () => {
|
|
159
|
+
return toBufferBE(largeValue, 8);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const b1 = Buffer.from('0123456789ABCDEF0123456789ABCDEF', 'hex');
|
|
163
|
+
const b2 = Buffer.from('0123456789ABCDEF0123456789ABCDEF', 'hex');
|
|
164
|
+
const bn1 = new BN('0123456789ABCDEF0123456789ABCDEF', 'hex');
|
|
165
|
+
const bn2 = new BN('0123456789ABCDEF0123456789ABCDEF', 'hex');
|
|
166
|
+
const n1 = 0x0123456789ABCDEF0123456789ABCDEFn;
|
|
167
|
+
const n2 = 0x0123456789ABCDEF0123456789ABCDEFn;
|
|
168
|
+
suite.add('Buffer equality comparison', () => {
|
|
169
|
+
return b1.compare(b2) === 0;
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
suite.add('BN equality comparison', () => {
|
|
173
|
+
return bn1.eq(bn2);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
suite.add('bigint equality comparison', () => {
|
|
177
|
+
return n1 === n2;
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
suite.add('BN multiply', () => {
|
|
181
|
+
return bn1.mul(bn2);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
suite.add('bigint multiply', () => {
|
|
185
|
+
return n1 * n2;
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
//#endregion
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
// Reporter for each benchmark
|
|
192
|
+
suite.on('cycle', (event: benchmark.Event) => {
|
|
193
|
+
const benchmarkRun: BenchmarkRun = event.target as BenchmarkRun;
|
|
194
|
+
const stats = benchmarkRun.stats as benchmark.Stats;
|
|
195
|
+
const meanInNanos = (stats.mean * 1000000000).toFixed(2);
|
|
196
|
+
const stdDevInNanos = (stats.deviation * 1000000000).toFixed(3);
|
|
197
|
+
const runs = stats.sample.length;
|
|
198
|
+
const ops = benchmarkRun.hz.toFixed(benchmarkRun.hz < 100 ? 2 : 0);
|
|
199
|
+
const err = stats.rme.toFixed(2);
|
|
200
|
+
|
|
201
|
+
console.log(`${benchmarkRun.name}: ${ops}±${err}% ops/s ${meanInNanos}±${
|
|
202
|
+
stdDevInNanos} ns/op (${runs} run${runs === 0 ? '' : 's'})`);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// Runs the test suite
|
|
206
|
+
suite.run();
|